【发布时间】:2014-02-06 21:08:43
【问题描述】:
我想在右侧创建一个带有复选框的按钮。 我试过这个,但复选框停留在按钮标签文本顶部的按钮中心。
欢迎提出任何想法。
提前致谢:
public class MainTest extends JPanel {
JButton button;
JPanel panel;
public MainTest() {
createComponents();
layoutComponents();
}
public void createComponents() {
// attempting to add checkbox to button
button = new JButton("Print with Edge");
JCheckBox checkBox = new JCheckBox();
jcb.setHorizontalAlignment(SwingConstants.RIGHT);
button.add(checkBox,new BorderLayout());
panel = new JPanel(new BorderLayout());
}
public void layoutComponents() {
panel.add(button,BorderLayout.SOUTH);
add(panel);
}
public static void main(String[] args) {
MainTest demo = new MainTest();
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.add(demo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
【问题讨论】:
-
您误用“叶子”组件作为容器,不要;-)
-
尝试使用
JToggleButton
标签: java swing components jbutton jcheckbox