我想在我的程序在這裏對齊RadioButtons。有人能幫助我嗎?我不確定如何去做。我在這裏和其他地方搜索,似乎沒有找到一個適用的例子。這是,我相信我的代碼的相關部分。如果需要更多,我會編輯。如何對齊JSplitPane中的RadioButtons?
final class SplitPanel extends JFrame {
private FlowLayout flowLayout = new FlowLayout();
private GridLayout gridLayout = new GridLayout(4, 1);
private DiagLayout diagLayout = new DiagLayout();
private JRadioButton jrbFlowLayout = new JRadioButton("Horizontal");
private JRadioButton jrbGridLayout = new JRadioButton("Verticle");
private JRadioButton jrbDiagLayout = new JRadioButton("Diagonal");
private JButton jbt1 = new JButton("Button 1");
private JButton jbt2 = new JButton("Button 2");
private JButton jbt3 = new JButton("Button 3");
private JButton jbt4 = new JButton("Button 4");
private JSplitPane jSplitPane;
private JPanel jPanel1, jPanel2;
public SplitPanel() {
this.setTitle("Split Panel with Diagonalization");
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createPanel1();
createPanel2();
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
jPanel1, jPanel2);
jSplitPane.setOneTouchExpandable(true);
jSplitPane.setContinuousLayout(true);
jSplitPane.setDividerLocation(150);
getContentPane().add(jSplitPane);
}
public void createPanel1() {
jPanel1 = new JPanel();
jPanel1.setBorder(new TitledBorder("Select a Layout Manger"));
jPanel1.add(jrbFlowLayout);
jPanel1.add(jrbGridLayout);
jPanel1.add(jrbDiagLayout);
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(jrbFlowLayout);
buttonGroup1.add(jrbGridLayout);
buttonGroup1.add(jrbDiagLayout);
}
public void createPanel2() {
jPanel2 = new JPanel();
jPanel2.setLayout(diagLayout);
jPanel2.add(jbt1);
jPanel2.add(jbt2);
jPanel2.add(jbt3);
jPanel2.add(jbt4);
jrbFlowLayout.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jPanel2.setLayout(flowLayout);
jPanel2.validate();
}
});
jrbGridLayout.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jPanel2.setLayout(gridLayout);
jPanel2.validate();
}
});
jrbDiagLayout.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jPanel2.setLayout(diagLayout);
jPanel2.validate();
}
});
}
謝謝你的幫忙!
什麼是DiagLayout? –