此代码有什么问题? (JScrollPanel和JFrame)

问题描述 投票:0回答:2

出于某种原因,当我运行此代码时,我的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时JFrame会不断塌陷。如果您也知道该如何解决,请告诉我!我将非常感谢。)

java jframe jpanel
2个回答
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(800, 800);

        frame.setVisible(true);



    }

    private void makeContent() {

        contentPane = (JPanel) frame.getContentPane();

        contentPane.setLayout(new BorderLayout(10, 10));

        makePanel();

    }

    private void makePanel() {

        JPanel panel = new JPanel();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JTextArea textarea = new JTextArea(5, 15);

        JScrollPane scroll = new JScrollPane(textarea);

        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        panel.add(scroll);

        contentPane.add(panel, BorderLayout.EAST);

    }

0
投票

手动设置绑定范围不是可行的做法。而是将适当的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发布问题或答案时请务必考虑
© www.soinside.com 2019 - 2024. All rights reserved.