我正在尝试向我的JTextarea添加滚动条,但滚动条不会显示我的Jtoolbar 任何人都可以告诉我这个代码有什么问题。所以我可以解决它。我一直在寻找,但滚动窗格stil没有出现
public PlayerGui() {
// create main windows
super("Liste");
JTextArea editors = new JTextArea();
editors.setLineWrap(true);
editors.setWrapStyleWord(true);
// scroll bar
JScrollPane scroll = new JScrollPane(editors);
setEditor(editors);
// create center panel
JPanel cent = new JPanel();
//create Panel for the to
JPanel north = new JPanel();
setNorthpanel(north);
// create tool bar
JToolBar toolbar = new JToolBar();
toolbar.add(scroll);
// set center panel and add preffered layout and backgrounds and size
setCenter(cent);
getCenter().setLayout(new BorderLayout());
// add scroll bar and toolbar
add(scroll, BorderLayout.EAST);
add(toolbar, BorderLayout.SOUTH);
//getCenter().setBackground(Color.black);
Dimension size = new Dimension(getCenter().getPreferredSize());
getEditor().setPreferredSize(size);
getCenter().getPreferredSize();
getCenter().setBorder(new CompoundBorder(new EmptyBorder(10,10 ,10,10),new EtchedBorder(Color.BLACK, Color.black)));
//add text editor to the center panel
getCenter().add(getEditor(),BorderLayout.CENTER);
//set layout of the frame
setLayout(new BorderLayout());
menubar1 = new JMenuBar();
//create menu list from a string arrays
for(int i=0; i<list.length; i++){
JMenu menus = new JMenu(list[i]);
menubar1.add(menus);
}
答案 0 :(得分:0)
您错误地使用了布局管理器。请找到正在运行的修改后的代码。
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFrame() {
// create main windows
super("Liste");
// scroll bar
JScrollPane scroll = new JScrollPane();
//setEditor(editors);
// create center panel
JPanel cent = new JPanel();
//create Panel for the to
JPanel north = new JPanel();
getContentPane().setLayout(new BorderLayout(0, 0));
//toolbar.add(scroll);
// set center panel and add preffered layout and backgrounds and size
//setCenter(cent);
//getCenter().setLayout(new BorderLayout());
// add scroll bar and toolbar
getContentPane().add(scroll);
JTextArea textArea = new JTextArea();
scroll.setViewportView(textArea);
JToolBar toolBar = new JToolBar();
getContentPane().add(toolBar, BorderLayout.NORTH);
JMenuBar menubar1 = new JMenuBar();
//create menu list from a string arrays
// for(int i=0; i<list.length; i++){
// JMenu menus = new JMenu(list[i]);
// menubar1.add(menus);
// }
}
}
希望这会有所帮助。 : - )