gpt4 book ai didi

java - JPanel 不显示按钮

转载 作者:行者123 更新时间:2023-12-01 16:34:43 25 4
gpt4 key购买 nike

我正在尝试让 JPanel 显示一系列按钮。每个按钮上都有与我的数独板值关联的值之一。我已经创建了我的板,添加了一个菜单,现在我尝试在同一个 Jframe 中显示菜单下方但板之前的 optional 。我希望我可以将所有按钮放在 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);
}
}
};

最佳答案

我在工具栏上使用了 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 中。

一个建议:

Avoid AWT components as much as possible

关于java - JPanel 不显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10488266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com