0
我想創建一個複選框在其右側的按鈕。 我試過這個,但複選框停留在按鈕標籤文本頂部按鈕的中心。如何將複選框添加到jButton的右側?
任何想法welkom。
在此先感謝:
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);
}
}
什麼是'jcb'? – Thomas
您錯誤地將「leaf」組件用作容器,請勿;-) – kleopatra
嘗試使用'JToggleButton' – ufis