GUI에서 레이아웃을 완료했지만 작동하지 않아 모든 구성 요소가 차례로 나타납니다. 당신이 JPanel의의의 레이아웃을 설정하지 마십시오레이아웃 관리자가 작동하지 않습니다
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();
}
`
GroupLayout는 개인적으로 BoxLayout에서는 몇 가지 심각한 중첩 된 구성 요소없이 물건을 처리하지 않습니다 같은 GridBagLayout에 또는 BoxLayout에서는 – pstanton
같은 다른 레이아웃 매니저를 사용하여 손으로 코드를 거라고, GUI 구축 도구 설계되었습니다. GridBagLayout을 사용할 수도 있지만 많은 사람들을 협박하는 것으로 이해할 수 있습니다. –
네, 개인적인 취향이지만, GroupLayout이 더 협박하는 손짓을 발견합니다! – pstanton