JButton没有出现

时间:2015-06-24 09:55:08

标签: java swing user-interface jbutton

我不明白为什么JButton没有出现。 这是by tutorial,一切似乎都没问题。

尝试将setVisible添加到各个地方 - 无论如何都没有正面结果。

   import javax.swing.*;

import java.awt.*;
import java.util.Random;
import java.awt.event.*;

@SuppressWarnings("serial")
public class testFly extends JFrame
{

     JButton startDrawing;
     int windowWidth = 900;
     int windowHeight = 500;

     Color[] shapeColor = {Color.orange, Color.red, Color.yellow, Color.blue, Color.pink, Color.cyan, Color.black, Color.green, Color.GRAY};

     public static void main(String[] args)
     {
        new testFly();

     }
     public testFly()
     {
        this.setSize(windowWidth, windowHeight);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Flyweight Test");

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        final JPanel drawingPanel = new JPanel();

        startDrawing = new JButton("Draw Stuff");
        startDrawing.setVisible(true);

        contentPane.add(drawingPanel, BorderLayout.CENTER);
        contentPane.add(drawingPanel, BorderLayout.SOUTH);

        startDrawing.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent event)
           {
              Graphics g = drawingPanel.getGraphics();
              long startTime = System.currentTimeMillis();

              for(int i=0; i < 100000; i++)
              {
                 Rect rect = RectFactory.getRect(getRandColor());
                 rect.draw(g, getRandX(), getRandY(), getRandX(), getRandY());
                 /*
               Rect rect = new Rect(getRandColor(), getRandX(), getRandY(), getRandX(), getRandY()) ;
               rect.draw(g);
               */
              }
              long endTime = System.currentTimeMillis();
              System.out.println("That took "+(endTime - startTime));
           }
        });
        this.add(contentPane);
        this.setVisible(true);
     }
     private Color getRandColor(){
        Random randomGenerator = new Random();
        int randInt = randomGenerator.nextInt(9);
        return shapeColor[randInt];
     }
     private int getRandX()
     {
        return (int)(Math.random()*windowWidth);
     }
     private int getRandY()
     {
        return (int)(Math.random()*windowHeight);
     }

}

5 个答案:

答案 0 :(得分:1)

你还没有添加按钮。添加它。你要添加drawingPanel两次。可能你试图向南添加按钮

contentPane.add(drawingPanel, BorderLayout.CENTER);
contentPane.add(startDrawing, BorderLayout.SOUTH);

答案 1 :(得分:1)

更改

contentPane.add(drawingPanel, BorderLayout.CENTER);
contentPane.add(drawingPanel, BorderLayout.SOUTH);

contentPane.add(drawingPanel, BorderLayout.CENTER);
contentPane.add(startDrawing, BorderLayout.SOUTH);

正如我之前的 评论中所提到的,您正在两次添加drawingPanel(可能是无意中)。因此,更改其中一行以添加Jbutton,它应该可以工作。

答案 2 :(得分:0)

您需要将按钮添加到contentPane:

contentPane.add(startDrawing, BorderLayout.NORTH); //Or wherever you want it
this.add(contentPane);
this.setVisible(true);

答案 3 :(得分:0)

  1. 您没有将JButton添加到contentPane
  2. 您的JButton没有大小,因此只要将其添加到JPanel就不会出现。
  3. 使用类似的东西:

    startDrawing.setBounds(50, 50, 200, 200);
    contentPane.add(startDrawing);
    

答案 4 :(得分:0)

您尚未向任何面板添加按钮

我已在您的代码中添加以下行并且它可以正常工作。

第36行:

startDrawing = new JButton("Draw Stuff");
startDrawing.setVisible(true);
drawingPanel.add(startDrawing);
drawingPanel.setVisible(true);
contentPane.add(drawingPanel, BorderLayout.CENTER);
contentPane.add(drawingPanel, BorderLayout.SOUTH);