當我試圖把ButtonGroup
反對我Box
對象,編譯器返回以下錯誤:添加的ButtonGroup到箱編排對象
no method for such a type
請幫我,我怎麼可以加我ButtonGroup
到橫向盒裏?
當我試圖把ButtonGroup
反對我Box
對象,編譯器返回以下錯誤:添加的ButtonGroup到箱編排對象
no method for such a type
請幫我,我怎麼可以加我ButtonGroup
到橫向盒裏?
事情是這樣的:
ButtonGroup bg; // your button group
Box box; // your box
// Create a panel to group the buttons.
JPanel panel = new JPanel();
// Add all of the buttons in the group to the panel.
for (Enumeration<AbstractButton> en = buttonGroup.getElements(); en.hasMoreElements();) {
AbstractButton b = en.nextElement();
panel.add(b);
}
// Add the panel to the box.
box.add(panel):
的ButtonGroup的擴展對象;它不是一個組件。所以它沒有明確地添加到容器或組件。相反,它將AbstractButton實例分組。
下面是從Java文檔的example code。不做ButtonGroup的一個組件(也可能實現這種方式的原因)的
一個好處是,你可以有不同的組件AbstractButton的情況是相同的ButtonGroup中的成員。
下面是一些示例代碼來演示它,使用的BoxLayout。
JPanel mainPanel = new JPanel();
mainPanel.setLayout (new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
ButtonGroup group = new ButtonGroup();
JButton dogButton = new JButton("dog");
group.add(dogButton);
JPanel dogPanel = new JPanel();
dogPanel.add(dogButton);
mainPanel.add(dogPanel);
JButton catButton = new JButton("cat");
group.add(catButton);
JPanel catPanel = new JPanel();
catPanel.add(catButton);
mainPanel.add(catPanel);
這裏發表您的代碼,以幫助你,找到錯誤 – 2011-12-02 13:23:24