简体   繁体   English

为什么我需要将鼠标悬停在按钮上才能显示?

[英]Why do I need to hover my mouse over a button for it to show up?

I got a issue here, my buttons were working fine until I added a background image, when I try to put my mouse over the buttons and textfield for them to show. 我在这里遇到了一个问题,我的按钮工作正常,直到我添加了背景图像,当我尝试将鼠标放在按钮和文本字段上以供显示时。 My labels also don't work either regardless if my mouse is clicking it or on it? 无论我的鼠标是点击它还是点击它,我的标签也不起作用? Heres the code to the class. 这是该类的代码。

public class launcher extends JFrame{
    private static final long serialVersionUID = 1L;

     protected JPanel window = new JPanel();
     protected JFrame f = new JFrame("stackoverflow");

    private Rectangle rAncient, rMedieval, rModern, rFuture, rFinancial, rUpdate;
    private JButton ancient, medieval, modern, future, financial, update;
    private JLabel time, name;
    private JTextField tName;


     protected int width = 600;
     protected int height = 400;
     protected int button_width = 80;
     protected int button_height = 40;


    public launcher(int id) throws IOException{
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e){
            e.printStackTrace();
        }
        setTitle("Choose your Path");
        setSize(new Dimension(width, height));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().add(window);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        window.setLayout(null);
        if(id==0){
        drawButtons();
    }
        repaint();
    }
    private void drawButtons(){
        ancient = new JButton("Ancient");
        rAncient = new Rectangle(140, 150, 70, 40);
        ancient.setBounds(rAncient);
        window.add(ancient);

        medieval = new JButton("Medieval");
        rMedieval = new Rectangle(220, 150, 80, 40);
        medieval.setBounds(rMedieval);
        window.add(medieval);

        modern = new JButton("Modern");
        rModern = new Rectangle(310, 150, 70, 40);
        modern.setBounds(rModern);
        window.add(modern);

        future = new JButton("Future");
        rFuture = new Rectangle(350, 150, 70, 40);
        future.setBounds(rFuture);
        window.add(future);

        financial = new JButton("Financial");
        rFinancial = new Rectangle(390, 150, 70, 40);
        future.setBounds(rFinancial);
        window.add(financial);

        update = new JButton("Update");
        rUpdate = new Rectangle(250, 300, 100, 50);
        update.setBounds(rUpdate);
        window.add(update);

        time = new JLabel("Choose your desired time period");
        time.setBounds(220, 90, 200, 50);
        window.add(time);

        name = new JLabel("Name: ");
        name.setBounds(210, 220, 200, 50);
        window.add(name);

        tName = new JTextField();
        tName.setBounds(250, 235, 150, 20);
        window.add(tName);
        tName.setText("Bob");

          ancient.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Ancient mode...");
            }
          });

          medieval.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Medieval mode...");
            }
          });

          modern.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Modern mode...");
            }
          });

          future.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Future mode...");
            }
          });

          financial.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Financial mode...");
            }
          });

          update.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Opening browser to http://ubergamesproductions.weebly.com/adventure");
                try{
                    String URL ="http://ubergamesproductions.weebly.com/adventure";
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e.getMessage());
                }
            }
          });
    }
    public static void main(String[] args)throws IOException{
    new launcher(0);
    }
      private Image backgroundImage = ImageIO.read(new File("C:/Users/Samuels Laptop/Dropbox/Java/Questor/Launcher_UGP.png"));
      public void paint(Graphics g) { 
        super.paint(g);
        g.drawImage(backgroundImage, 0, 0, null);
      }
};

public launcher(int id) throws IOException{ public launcher(int id)抛出IOException {

Use standard Java naming conventions. 使用标准Java命名约定。 Class names should start with an upper case character. 类名应以大写字符开头。

window.setLayout(null);
...
ancient = new JButton("Ancient");
rAncient = new Rectangle(140, 150, 70, 40);
ancient.setBounds(rAncient);

Don't use a null layout and setBounds(...). 不要使用null布局和setBounds(...)。 Use a proper layout manager and let the layout manager do its job. 使用适当的布局管理器,让布局管理器完成其工作。 In this case you can probably use a JPanel and its default FlowLayout. 在这种情况下,您可以使用JPanel及其默认的FlowLayout。

public void paint(Graphics g) { 
    super.paint(g);
    g.drawImage(backgroundImage, 0, 0, null);

Don't override the paint() method of a top level container. 不要覆盖顶级容器的paint()方法。 In your case your code paints the frame and all of the components. 在您的情况下,您的代码绘制框架和所有组件。 Then you draw the image. 然后你绘制图像。 So the image covers the components. 因此图像涵盖了组件。 The button respondes to mouseEntered events to repaint() the border so that is why it suddenly appears. 该按钮响应mouseEntered事件以重绘()边框,这就是它突然出现的原因。

Instead, custom painting is done by overriding the paintComponent(...) method of a JPanel. 而是通过覆盖JPanel的paintComponent(...)方法来完成自定义绘制。 Then you add the panel to the frame and you add your components to the panel. 然后将面板添加到框架中,然后将组件添加到面板中。 Read the section from the Swing tutorial on Custom Painting for more information. 有关详细信息,请阅读自定义绘画的Swing教程中的部分。 Take the time to read the whole tutorial. 花点时间阅读整个教程。 There is also a section on Layout Managers . 还有一个关于Layout Managers的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM