每当我运行我的程序时,我的JTextArea都不会遵循我给它的维度,但如果我调整我的JFrame大小,它会更新并将其大小设置为我放置的大小。
问题是什么?
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// set sizes
userspane.setSize(100, 500);
jta.setSize(100, 500);
list.setSize(100, 500);
consolepane.setSize(100, 500);
console.setSize(100, 500);
// add to panel
panel.add(userspane, BorderLayout.CENTER);
panel.add(kick);
panel.add(ban);
panel.add(info);
panel.add(consolepane, BorderLayout.CENTER);
// set frame properties
setTitle("RuneShadows CP");
setSize(280, 400);
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
}
答案 0 :(得分:3)
不要将尺寸设置为任何。
JTextArea
,您可以使用构造函数JTextArea(int rows, int charSpaces)
pack()
JFrame
,它就会尊重内部组件的所有首选尺寸。pack()
我不确定哪个变量是什么(或者你想要的大小),所以我假设了文本区域,还有其他区域。请参阅此示例,其中我刚刚使用了我提到的JTextArea
构造函数,并且只使用了pack
ed。
已编辑
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ControlPanel extends JFrame {
JScrollPane userspane;
JList list;
DefaultListModel model = new DefaultListModel();
JScrollPane consolepane;
JTextArea console = new JTextArea(20, 50);
JTextArea jta = new JTextArea(6, 50);
JPanel panel = new JPanel();
JButton kick = new JButton("Kick");
JButton ban = new JButton("Ban");
JButton info = new JButton("Info");
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// add to panel
panel.add(userspane, BorderLayout.CENTER);
panel.add(kick);
panel.add(ban);
panel.add(info);
panel.add(consolepane, BorderLayout.CENTER);
add(panel);
pack();
setTitle("RuneShadows CP");
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ControlPanel();
}
});
}
}
更新 - 定位
请注意,BorderLayout
您需要为添加的每个组件指定一个位置,否则默认为CENTER
,每个位置只有一个组件。我注意到您尝试向CENTER
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ControlPanel extends JFrame {
JScrollPane userspane;
JList list;
DefaultListModel model = new DefaultListModel();
JScrollPane consolepane;
JTextArea console = new JTextArea(20, 50);
JTextArea jta = new JTextArea(6, 50);
JPanel panel = new JPanel(new BorderLayout());
JButton kick = new JButton("Kick");
JButton ban = new JButton("Ban");
JButton info = new JButton("Info");
public ControlPanel() {
// create our list of players
list = new JList(model);
// create our scroll panes
userspane = new JScrollPane(list);
consolepane = new JScrollPane(console);
// add to panel
panel.add(userspane, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(kick);
buttonPanel.add(ban);
buttonPanel.add(info);
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(consolepane, BorderLayout.NORTH);
add(panel);
pack();
setTitle("RuneShadows CP");
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ControlPanel();
}
});
}
}