我正在尝试让JPanel显示一系列按钮。这些按钮中的每一个都是与我的数独棋盘值相关联的值之一。我创建了我的电路板添加了一个菜单,现在我试图在菜单下面但在电路板之前显示可选择的选项..在同一个Jframe中。我希望我可以将所有按钮放到JPanel上并将该面板放到Frame上。它将显示JPanel但不显示任何按钮。有一次我把面板展示出来,但没有一个是大小的,而且还有很多。我必须更具体的问题是我使用正确的代码来显示我的JPanel上的一系列按钮,这些按钮位于包含我的数独板的框架中,该框架也是一系列按钮。
这个JFrame的最终工具栏和按钮是多少,这就是为什么它不起作用?无论如何这里只是我的工具栏的代码,这是我的JPanel。
class ToolBar extends JPanel {
// instance initializer (constructor equivalent)
public ToolBar() {
super();
this.setLayout(myLayout);
myLayout.setAlignment(FlowLayout.TRAILING);
Button[] panelButton = new Button[size];
//Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON);
//setBounds(rec);
setPreferredSize(new Dimension(330, 45));
for(int i = 0; i < size; i++) {
Rectangle r = new Rectangle(12, 12, 22, 22);
center = new ImageIcon(view.drawSymbol(i));
panelButton[i]= new Button();
panelButton[i].setIcon(center);
panelButton[i].setOpaque(true);
panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black));
panelButton[i].setBounds(r);
this.add(panelButton[i]);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setVisible(true);
}
}
};
答案 0 :(得分:4)
我在工具栏上使用setBounds
使其可见,并将背景设置为红色仅用于测试,用Swing JButtons替换AWT按钮,我还在按钮上设置了一些文本。我在测试代码上注释了一些东西,以便编译,但是将它们放回到下面:
class ToolBar extends JPanel {
// instance initializer (constructor equivalent)
public ToolBar() {
super();
this.setLayout(myLayout);
myLayout.setAlignment(FlowLayout.TRAILING);
JButton[] panelButton = new JButton[5];
this.setBackground(Color.red);
this.setBounds(0, 0, 200, 200);
//Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON);
//setBounds(rec);
setPreferredSize(new Dimension(330, 45));
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(22, 22);
panelButton[i] = new JButton();
panelButton[i].setText(" ");
panelButton[i].setIcon(new ImageIcon(view.drawSymbol(i)));
panelButton[i].setOpaque(true);
panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black));
panelButton[i].setBounds(r);
this.add(panelButton[i]);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setVisible(true);
}
}
};
我也在下面发布整个测试代码:
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
/**
*
* @author hahahaha
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
this.add(new ToolBar());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
class ToolBar extends JPanel {
// instance initializer (constructor equivalent)
public ToolBar() {
super();
//this.setLayout(myLayout);
//myLayout.setAlignment(FlowLayout.TRAILING);
JButton[] panelButton = new JButton[5];
this.setBackground(Color.red);
this.setBounds(0, 0, 200, 200);
//Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON);
//setBounds(rec);
setPreferredSize(new Dimension(330, 45));
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(22, 22);
//center = new ImageIcon(view.drawSymbol(i));
panelButton[i] = new JButton();
panelButton[i].setText(" ");
//panelButton[i].setIcon(center);
panelButton[i].setOpaque(true);
panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black));
panelButton[i].setBounds(r);
this.add(panelButton[i]);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setVisible(true);
}
}
};
}
查找我实例化的行this.add(new ToolBar());
,并将工具栏添加到我的JFrame。
一条建议:
尽可能避免使用AWT组件
答案 1 :(得分:2)
不要使用setPreferredSize或setBounds;让LayoutManager为您处理位置和尺寸。
根据您的需要,您可能需要考虑使用JToolBar而不是实现自己的。