Java – why can’t the content of my JFrame be displayed correctly?

In order to reduce the code, I am taking out the code irrelevant to the problem, such as addactionlistener(); wait

My main course is the public course TF2_ Account_ Chief

I have my Lord JFrame f; And its contents:

private static JFrame f = new JFrame("TF2 Account C.H.I.E.F.");
private JLabel runnableTogetherLabel = new JLabel("How many idlers would you like to run at a time?");
private static JTextField runnableTogetherInput = new JTextField();
private JButton runButton = new JButton("Run!");
private JButton stopButton = new JButton("Stop!");
private JButton resetButton = new JButton("Reset!");
private JButton exitButton = new JButton("Exit!");

I set the properties of all content:

public void launchFrame() {
        f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
        f.add(runnableTogetherInput);
        f.add(runnableTogetherLabel);
        f.add(runButton);
        f.add(stopButton);
        f.add(resetButton);
        f.add(exitButton);
        f.setSize(625,500);
        runnableTogetherInput.setSize(35,20);
        runnableTogetherLabel.setSize(275,25);
        runButton.setSize(60,25);
        stopButton.setSize(65,25);
        resetButton.setSize(70,25);
        exitButton.setSize(60,25);
        f.setLocation(0,0);
        runnableTogetherInput.setLocation(285,3);
        runnableTogetherLabel.setLocation(5,0);
        runButton.setLocation(330,0);
        stopButton.setLocation(395,0);
        resetButton.setLocation(465,0);
        exitButton.setLocation(540,0);
    }

Then I have my main () method:

public static void main(String[] args) throws IOException {
    TF2_Account_Chief gui = new TF2_Account_Chief();
    gui.launchFrame();
    Container contentPane = f.getContentPane();
    contentPane.add(new TF2_Account_Chief());
}

Then I have my second JFrame if does not display the content correctly:

private void invalidinput() {
    JFrame iF = new JFrame("Invalid Input!");
    JLabel iL = new JLabel("The input you have entered is invalid!");
    JButton iB = new JButton("Exit!");
    Container contentPane2 = iF.getContentPane();
    contentPane2.add(new TF2_Account_Chief());
    iF.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE);
    iF.pack();
    iF.setVisible(true);
    iF.add(iL);
    iF.add(iB);
    iF.setSize(500,300);
    iL.setSize(125,25);
    iB.setSize(60,25);
    iF.setLocation(0,0);
    iL.setLocation(0,15);
    iB.setLocation(0,45);
}

Now, calling the invalidinput () method will start JFrame if, but you can't see it because this part of the code is not related to the problem The important thing is the introduction of JFrame if

The new framework is as follows:

Any idea why the content is not displayed correctly?

(incorrectly, I mean JButton IB occupies the whole frame, and the frame is light blue instead of ordinary gray.)

Solution

You use absolute positions instead of empty layouts, which is why you see a big button

To view each component, you must use

iF.setLayout(null);

But this is not a good habit. I suggest you learn how to use layouts and leave all the work to the layout administrator

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>