布局管理器不起作用

发布于 2024-08-17 13:47:26 字数 3206 浏览 9 评论 0原文

我已经在 GUI 中完成了布局,但它不起作用,所有组件都一个接一个地出现。

这是我的代码:

import javax.swing.*;
public class main extends JFrame {
public main() {
    try {

        add(new FYP_Tx.GUI());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("FYP Video Platform");
        setResizable(true);
        setVisible(true);

    } catch (Exception e) {
        System.out.println("Exception: " + e.toString());
    }
}

public static void main(String[] args) {
    new main();
}
}



public class GUI extends JPanel implements Runnable, ActionListener, ItemListener {

private JButton btnStart, btnStop, btnPause, btnFile;
private JLabel lblDisplay, lblSNR, lblStatus, lblConfig;
private JCheckBox chkLoop, chkNeg;
private Thread animator;
private JFileChooser fileChooser;
private JList lstML;
private JTextField txtSNR;
private boolean pauseAnimator, loop;
Matlab_options matlab = new Matlab_options();

public GUI() {
    GroupLayout guiLayout = new GroupLayout(this);

    lblDisplay = new JLabel();
    lblStatus = new JLabel();
    lblConfig = new JLabel();
    lblSNR = new JLabel("SNR: ");
    btnStart = new JButton("Start");
    btnStop = new JButton("Stop");
    btnPause = new JButton("Pause");
    btnFile = new JButton("Open File");
    chkLoop = new JCheckBox("Loop");
    chkNeg = new JCheckBox("Negative SNR");
    txtSNR = new JTextField(3);
    txtSNR.setText("10");
    lblDisplay.setDoubleBuffered(true);

    btnStart.setEnabled(false);
    btnStop.setEnabled(false);
    btnPause.setEnabled(false);

    btnStart.setActionCommand("start");
    btnStop.setActionCommand("stop");
    btnPause.setActionCommand("pause");
    btnFile.setActionCommand("file");

    guiLayout.setAutoCreateContainerGaps(true);
    guiLayout.setAutoCreateGaps(true);
    GroupLayout.SequentialGroup hGroup = guiLayout.createSequentialGroup();
    GroupLayout.SequentialGroup vGroup = guiLayout.createSequentialGroup();

    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(lblDisplay));
    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(btnStart).addComponent(btnStop).addComponent(btnPause).addComponent(chkLoop));
    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(btnFile).addComponent(chkLoop));
    guiLayout.setHorizontalGroup(hGroup);

    vGroup.addGroup(guiLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lblDisplay).addComponent(btnStart).addComponent(btnFile));
    vGroup.addGroup(guiLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(btnStop).addComponent(chkLoop));
    guiLayout.setVerticalGroup(vGroup);

    btnStart.addActionListener(this);
    btnStop.addActionListener(this);
    btnPause.addActionListener(this);
    btnFile.addActionListener(this);
    chkLoop.addItemListener(this);
    chkNeg.addItemListener(this);
}

public void addNotify() {
    super.addNotify();
    loop = false;
    lblDisplay.setSize(400, 400);
    lblDisplay.setVisible(true);
    animator = new Thread(this);
}

public void paint(Graphics g) {
    super.paint(g);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

`

I have done an layout in my GUI but it did not work , all the component just appear one after another.

Here is my codes :

import javax.swing.*;
public class main extends JFrame {
public main() {
    try {

        add(new FYP_Tx.GUI());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("FYP Video Platform");
        setResizable(true);
        setVisible(true);

    } catch (Exception e) {
        System.out.println("Exception: " + e.toString());
    }
}

public static void main(String[] args) {
    new main();
}
}



public class GUI extends JPanel implements Runnable, ActionListener, ItemListener {

private JButton btnStart, btnStop, btnPause, btnFile;
private JLabel lblDisplay, lblSNR, lblStatus, lblConfig;
private JCheckBox chkLoop, chkNeg;
private Thread animator;
private JFileChooser fileChooser;
private JList lstML;
private JTextField txtSNR;
private boolean pauseAnimator, loop;
Matlab_options matlab = new Matlab_options();

public GUI() {
    GroupLayout guiLayout = new GroupLayout(this);

    lblDisplay = new JLabel();
    lblStatus = new JLabel();
    lblConfig = new JLabel();
    lblSNR = new JLabel("SNR: ");
    btnStart = new JButton("Start");
    btnStop = new JButton("Stop");
    btnPause = new JButton("Pause");
    btnFile = new JButton("Open File");
    chkLoop = new JCheckBox("Loop");
    chkNeg = new JCheckBox("Negative SNR");
    txtSNR = new JTextField(3);
    txtSNR.setText("10");
    lblDisplay.setDoubleBuffered(true);

    btnStart.setEnabled(false);
    btnStop.setEnabled(false);
    btnPause.setEnabled(false);

    btnStart.setActionCommand("start");
    btnStop.setActionCommand("stop");
    btnPause.setActionCommand("pause");
    btnFile.setActionCommand("file");

    guiLayout.setAutoCreateContainerGaps(true);
    guiLayout.setAutoCreateGaps(true);
    GroupLayout.SequentialGroup hGroup = guiLayout.createSequentialGroup();
    GroupLayout.SequentialGroup vGroup = guiLayout.createSequentialGroup();

    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(lblDisplay));
    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(btnStart).addComponent(btnStop).addComponent(btnPause).addComponent(chkLoop));
    hGroup.addGroup(guiLayout.createParallelGroup().addComponent(btnFile).addComponent(chkLoop));
    guiLayout.setHorizontalGroup(hGroup);

    vGroup.addGroup(guiLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(lblDisplay).addComponent(btnStart).addComponent(btnFile));
    vGroup.addGroup(guiLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(btnStop).addComponent(chkLoop));
    guiLayout.setVerticalGroup(vGroup);

    btnStart.addActionListener(this);
    btnStop.addActionListener(this);
    btnPause.addActionListener(this);
    btnFile.addActionListener(this);
    chkLoop.addItemListener(this);
    chkNeg.addItemListener(this);
}

public void addNotify() {
    super.addNotify();
    loop = false;
    lblDisplay.setSize(400, 400);
    lblDisplay.setVisible(true);
    animator = new Thread(this);
}

public void paint(Graphics g) {
    super.paint(g);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2024-08-24 13:47:26

JPanel不需要设置Layoutmanager吗?

    public GUI() {
        GroupLayout guiLayout = new GroupLayout(this);
        this.setLayout(guiLayout);
 ......
    }

Don't you have to set the Layoutmanager for the JPanel?

    public GUI() {
        GroupLayout guiLayout = new GroupLayout(this);
        this.setLayout(guiLayout);
 ......
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文