3

コードレスのため、問題に関係のないコードなどを抜いていますaddActionListener();

私のメインクラスはpublic class TF2_Account_Chief

メインJframe f;とその内容があります:

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!");

すべてのコンテンツのプロパティを設定します。

    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);
    }

それから私は私のmain()方法を持っています:

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());
}

そしてJFrame iF、コンテンツを正しく表示していない2番目のものがあります:

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);
}

現在、メソッドが呼び出されJFrame iFたときに起動されinvalidInput()ますが、コードのその部分は問題とは無関係であるため、それを確認することはできません。重要なのは、JFrame iFが起動されることです。

新しいフレームは次のようになります。JFrame iF

コンテンツが正しく表示されない理由について何か考えはありますか?

(不適切とは、JButton iBがフレーム全体を占有し、フレームが通常のグレーではなく水色であることを意味します。)

4

2 に答える 2

9

レイアウトを使用せずに絶対位置を使用しているnullため、大きなボタンが表示されます。

すべてのコンポーネントを表示するには、使用する必要があります

iF.setLayout(null);

しかし、それは良い習慣ではありません。レイアウトの使用方法を学び、すべての作業をレイアウト マネージャーに任せることをお勧めします。

于 2013-07-05T10:57:25.343 に答える