由于某种原因,当我运行此代码时,我的JFrame变成空白。我可能已经尝试在线教程一个小时了,我想知道我是否误解了。
代码如下:
public class Application {
public static JFrame f;
public static JButton submit;
public static JTextField unscramblee;
public static String scrambledWord, possibleWords;
public static JLabel possibleWordsDisplay;
public static JPanel UI;
public static JScrollPane scrollPane;
Application() {
f = new JFrame("test");
f.setResizable(true);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UI = new JPanel();
scrollPane = new JScrollPane(UI);
unscramblee = new JTextField("test");
unscramblee.setBounds(240, 200, 400, 50);
submit = new JButton("Submit");
submit.setBounds(240, 350, 400, 100);
possibleWordsDisplay = new JLabel("possibleWordsDisplay - this is a display for words that are possible");
possibleWordsDisplay.setBounds(240, 0, 200, 200);
scrollPane.add(unscramblee);
scrollPane.add(submit);
scrollPane.add(possibleWordsDisplay);
f.getContentPane().add(scrollPane);
f.pack();
f.setSize(1280,720);
}
}
我希望这是足够的信息来提供帮助。谢谢。
(如果有人想知道为什么setSize方法在pack方法之后,这是因为JFrame在我运行时会不断崩溃。如果您也知道如何解决此问题,请告诉我!我将非常感谢。 )
答案 0 :(得分:1)
手动设置绑定范围不是可行的做法。
而是将适当的Layout Managers用于所需的布局。
以下是mre 1 (请注意注释):
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Application {
Application() {
JFrame f = new JFrame("test");
//f.setSize(1280,720); f.pack should automatically set the size
f.setResizable(true);
//f.setLayout(null); do not use null layout
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel UI = new JPanel(); // uses flowlayout by default
//UI.setBounds(0, 0, 1280, 720); do not set bounds. that the job of the layout manager
JTextField unscramblee = new JTextField("test", 10);
//unscramblee.setBounds(240, 200, 400, 50);
JButton submit = new JButton("Submit");
//submit.setBounds(240, 350, 400, 100);
JLabel possibleWordsDisplay = new JLabel("possibleWordsDisplay - this is a display for words that are possible");
//possibleWordsDisplay.setBounds(240, 0, 200, 200);
UI.add(unscramblee);
UI.add(submit);
UI.add(possibleWordsDisplay);
JScrollPane scrollPane = new JScrollPane(UI);
f.getContentPane().add(scrollPane); //uses borderlayout by default
f.pack();
f.setVisible(true); //make frame visible after construction is completed
}
public static void main(String[] args) {
new Application();
}
}
答案 1 :(得分:0)
创建GUI时,请考虑按照以下设计开始:
private JFrame frame;
private JPanel contentPane;
public static void main(String[] args) {
GUI gui = new GUI();
gui.start();
}
private void start() {
frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
makeContent();
frame.setSize(500, 130);
frame.setVisible(true);
}
private void makeContent() {
contentPane = (JPanel) frame.getContentPane();
makePanel();
}
private void makePanel() {
JPanel ui = new JPanel();
ui.setLayout(new BoxLayout(ui, BoxLayout.Y_AXIS));
JLabel possibleWordsDisplay = new JLabel("possibleWordsDisplay - this is a display for words that are possible");
ui.add(possibleWordsDisplay);
JTextField unscramblee = new JTextField();
JScrollPane scroll = new JScrollPane(unscramblee);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
ui.add(scroll);
JButton submit = new JButton("Submit");
ui.add(submit);
contentPane.add(ui, BorderLayout.CENTER);
}