我创建了一个扩展了JPanel的类。然后当我创建它的一个对象并添加到现有的JPanel时,它不会显示出来。我在这里做错了什么?
public class StartTower extends JPanel{
private int value;
public StartTower(int value){
this.value = value;
}
public static StartTower send(int value){
return new StartTower(value);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(752, 359);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(120, 60, 10, 270);
g.fillRect(20, 330, 210, 10);
g.fillRect(375, 60, 10, 270);
g.fillRect(275, 330, 210, 10);
g.fillRect(630, 60, 10, 270);
g.fillRect(530, 330, 210, 10);
int val1 = 20;
int val2 = 315;
int val3 = 210;
int col = 100;
for (int i = 0; i < value; i++) {
g.setColor(new Color(100, col, 100));
g.fillRect(val1, val2, val3, 15);
System.out.println(val1 + " " + val2 + " " + val3 + " " + col);
val1 += 10;
val2 -= 15;
val3 -= 20;
col -= 10;
}
}
}
使用JPanel扩展的类
private void startBtActionPerformed(java.awt.event.ActionEvent evt) {
//mainPanel.repaint();
name = javax.swing.JOptionPane.showInputDialog(rootPane, "Enter you name!", "Ready?", WIDTH);
if (name != null) {
clockFunc(true);
game.initialDisk((int) disksSpinner.getValue());
StartTower startTower = new StartTower((int) disksSpinner.getValue());
mainPanel.setLayout(null);
this.add(startTower);
a2bBt.setEnabled(true);
a2cBt.setEnabled(true);
b2aBt.setEnabled(true);
b2cBt.setEnabled(true);
c2aBt.setEnabled(true);
c2bBt.setEnabled(true);
optimumLabel.setText(getNoOfOptimumMoves((int) disksSpinner.getValue()));
disksSpinner.setEnabled(false);
startBt.setEnabled(false);
}
}
我创建前一个类的对象并将其添加到现有JPanel的方法。 (mainPanel是在NetBeans中使用拖放创建的现有JPanel)。
为什么不出现?代码有什么问题?提前谢谢!
答案 0 :(得分:1)
我不确定您的完整代码是什么,但我只是尝试了这个并且它运行良好:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StartTower extends JPanel
{
private int value;
public StartTower(int value)
{
this.value = value;
}
public static StartTower send(int value)
{
return new StartTower(value);
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(752, 359);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(120, 60, 10, 270);
g.fillRect(20, 330, 210, 10);
g.fillRect(375, 60, 10, 270);
g.fillRect(275, 330, 210, 10);
g.fillRect(630, 60, 10, 270);
g.fillRect(530, 330, 210, 10);
int val1 = 20;
int val2 = 315;
int val3 = 210;
int col = 100;
for (int i = 0; i < value; i++)
{
g.setColor(new Color(100, col, 100));
g.fillRect(val1, val2, val3, 15);
System.out.println(val1 + " " + val2 + " " + val3 + " " + col);
val1 += 10;
val2 -= 15;
val3 -= 20;
col -= 10;
}
}
private static void createAndShowGUI()
{
StartTower startTower = new StartTower(5);
JFrame jFrame = new JFrame();
JPanel jPanel = new JPanel();
jPanel.add(startTower);
jFrame.getContentPane().add(jPanel);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
答案 1 :(得分:0)
如果你打算使用我不建议使用的null布局,那么在添加组件之前,你必须设置它的边界,以便null布局知道放置它的位置。