有些東西我不明白。我的代碼顯然不喜歡JScrollBar
。我添加它,我不能水平滾動也不能垂直滾動。JScrollPane在JTextArea中不滾動
這裏是什麼樣子:
請記住,我是新的,我仍然在做這個工作,所以我很抱歉,如果這是真的東西明顯,容易避免。
public ChangeLog() {
//Init.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea textarea = new JTextArea();
JScrollPane scrollpane = new JScrollPane(textarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//Text Stuff
textarea.setFont(textarea.getFont().deriveFont(16f));
textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
+ "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
textarea.setForeground(Color.BLACK);
Dimension d = new Dimension(250, 275);
textarea.setPreferredSize(d);
//Other Stuff
scrollpane.setViewportView(textarea);
scrollpane.getPreferredSize();
//Layout
panel.setLayout(null);
scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));
//Frame Stuff
frame.setAlwaysOnTop(true);
frame.setSize(300, 350);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
//Panel Stuff
frame.add(panel);
panel.setSize(frame.getSize());
panel.setBackground(Color.BLUE);
panel.add(textarea);
panel.add(scrollpane);
} }
你不需要添加'textarea'只添加'scrollpane'看看這個[如何在文本區添加scrollPane](https://stackoverflow.com/a/8849094/5855946) –
1 )Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –
.. 3)請參閱[我是否應避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/q/7229226/418556)(是)4)它如果文本區域的行代替顯示爲「JList」中的條目,則可能對用戶更好。 5)源代碼中的單個空白行是所有需要的。 '{'之後或'}'之前的空行通常也是多餘的。 6)*「請記住,我是新的」*該代碼片段有太多不好的做法,我停止計數。 ... –