簡體   English   中英

計算器:按鈕僅在測試 JFrame 類中鼠標懸停在按鈕上時顯示

[英]Calculator: Buttons ONLY show up when mouse hovers over them in Test JFrame class

我很着急,所以 idc 關於重復。 我仍在努力學習 Java 和術語,直到本學期結束。 我用了一個模板。 我正在使用背景圖像(“面板”),這使一切變得復雜。

基本上,按鈕僅在我將鼠標懸停在它們上方時才會顯示。 顯然,它與 JPanels 有關系。

我排除了您可能會要求的代碼,希望這次有人可以幫助我,因為我的按鈕與我在查看其他推薦帖子時看到的不同。

另外,我可以將 JFrame 設為固定大小(Test 類代碼中的大小)嗎?

代碼可能是多余的,但我只是想讓一切正常。 請記住,我是 Java 新手。

測試類:

public class TestCalculator {    
    public static void main(String[] args) {
        JFrame frame = new JFrame(TestCalculator.class.getSimpleName());
        ImagePanel panel = new ImagePanel(new ImageIcon("01_Crane_AGweb.jpg").getImage());
        SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();        
        calc.SetColors(null , Color.white , new Color(72,61,139));
        calc.setVisible(true);
        calc.setOpaque(false);
        panel.setVisible(true);
        panel.setOpaque(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        calc.add(panel);
        frame.add(panel);
        frame.add(calc);       
        frame.getContentPane().add(calc);   
        frame.setPreferredSize(new Dimension(358,379));
        frame.setMinimumSize(new Dimension(358,379));
        frame.setVisible(true);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }
}
class ImagePanel extends JPanel {
    private Image img;
    public ImagePanel(String img) {
    this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
    this.img = img;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    this.setPreferredSize(new Dimension(size));
    this.setMinimumSize(new Dimension(size));
    this.setMaximumSize(new Dimension(size));
    this.setSize(new Dimension(size));
    this.setLayout(null);
}
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, 0, 0, this.getWidth(),this.getHeight(),this);
}
}

主要的:

    public class SimpleArithmeticCalculator extends JPanel implements ActionListener {

         //BUTTONSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS !
         ...
         // jpanels, buttons, font, values, etc

        public SimpleArithmeticCalculator() {

          super();

      //I THINK this is the problem:  

      buttonPanel.setForeground(null);
      textPanel.setForeground(null);
      calcPanel.setForeground(null);

      textPanel.setLayout(new GridLayout(0,1,0,0));
      buttonPanel.setLayout(new GridLayout(0 , 5 , 5 , 5));

      displayText = new JTextField("" , 20);
      displayText.setHorizontalAlignment(JTextField.RIGHT);
      displayText.setFont(font);
      displayText.setEditable(false);

      textPanel.add(displayText);
      buttons = new JButton[NUM_BUTTONS];

      for (int i = 0 ; i < NUM_BUTTONS ; ++i) {

          buttons[i] = new JButton("" + buttonTexts[i]);
          buttons[i].setMnemonic(buttonKeys[i]);
          buttons[i].setFont(font);
          buttons[i].setMinimumSize(new Dimension(50,50));
          buttons[i].setActionCommand("" + buttonTexts[i]);
          buttons[i].addActionListener(this);
          buttonPanel.add(buttons[i]);
      }

      buttons[BTN_POWER].setText("^");
      buttons[BTN_PERCENT].setText("%");

      buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      textPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      calcPanel.setLayout(new BorderLayout());
      calcPanel.add(textPanel , BorderLayout.NORTH);
      calcPanel.add(buttonPanel , BorderLayout.CENTER);
      add(calcPanel);

      setMinimumSize(new Dimension(358,379));

      setPreferredSize(new Dimension(359,381));

       for (int i = 0 ; i < NUM_BUTTONS ; ++i) {
           buttons[i].setMaximumSize(buttons[i].getSize());
       }

   }

     public void SetColors(Color bg , Color textbg , Color textcolor) {

       calcPanel.setBackground(bg);
       calcPanel.setVisible(true);
       calcPanel.setOpaque(false);
       buttonPanel.setBackground(bg);
       buttonPanel.setVisible(true);
       buttonPanel.setOpaque(false);
       textPanel.setBackground(bg);
       textPanel.setOpaque(true);
       textPanel.setVisible(true);
       displayText.setBackground(textbg);
       displayText.setForeground(textcolor);

       for (int i = 0 ; i < NUM_BUTTONS ; ++i) { 
            buttons[i].setForeground(textcolor);
       }

     }

     //ACTION PERFORMED STUFF & OPERATIONS, BLAH

     public boolean isOpCharacter(char c) {

          return ((c == buttonTexts[BTN_MULT]) ||
                  (c == buttonTexts[BTN_DIV]) ||
                  (c == buttonTexts[BTN_PLUS]) ||
                  (c == buttonTexts[BTN_MINUS]) ||
                  (c == buttonTexts[BTN_POWER]) ||
                  (c == buttonTexts[BTN_PERCENT]));
     }

     public boolean isNumericCharacter(char c) {

      return ((c == buttonTexts[BTN_ZERO]) ||
        (c == buttonTexts[BTN_ONE]) ||
        (c == buttonTexts[BTN_TWO]) ||
        (c == buttonTexts[BTN_THREE]) ||
        (c == buttonTexts[BTN_FOUR]) ||
        (c == buttonTexts[BTN_FIVE]) ||
        (c == buttonTexts[BTN_SIX]) ||
        (c == buttonTexts[BTN_SEVEN]) ||
        (c == buttonTexts[BTN_EIGHT]) ||
        (c == buttonTexts[BTN_NINE]) ||
        (c == buttonTexts[BTN_DECIMAL]));

     }

     public boolean isNonZeroNumber(char c) {

      return (c == buttonTexts[BTN_ONE] ||
        c == buttonTexts[BTN_TWO] ||
        c == buttonTexts[BTN_THREE] ||
        c == buttonTexts[BTN_FOUR] ||
        c == buttonTexts[BTN_FIVE] ||
        c == buttonTexts[BTN_SIX] ||
        c == buttonTexts[BTN_SEVEN] ||
        c == buttonTexts[BTN_EIGHT] ||
        c == buttonTexts[BTN_NINE]);

     }

        public static void main(String[] args) { }
 }

無需創建圖像面板類。 您可以使用 JLabel 來顯示 ImageIcon。

    frame.pack(); // this is the problem, invoke after all the components have been added
    calc.add(panel);
    frame.add(panel);
    frame.add(calc);       
    frame.getContentPane().add(calc);   
    frame.setPreferredSize(new Dimension(358,379)); 
    frame.setMinimumSize(new Dimension(358,379));
    frame.setVisible(true);

如果我不得不做一個瘋狂的猜測,我會說問題在於您在將所有組件添加到框架之前使用了 frame.pack() 。

創建框架的基本代碼應該是:

//  Create all the panel and add all the components to the panels

JPanel panel = new JPanel();
panel.add(..);

//  Create the frame and add all the panels to the frame.

JFrame frame = new JFrame(...);
frame.add( panel ;
frame.pack();
frame.setVisible(true);

更新:

另一個問題可能與以下代碼有關:

frame.add(panel);
frame.add(calc);

默認情況下,框架使用 BorderLayout。 當您將組件添加到框架時,它們默認添加到 CENTER,問題是一次只能將單個組件添加到 CENTER。 看起來您正在嘗試使用背景圖像。 如果是這種情況,那么正如我之前建議的那樣,您可以使用 JLabel 作為背景。 你的代碼應該是這樣的:

JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new BorderLayout() );

SimpleArithmeticCalculator calc = new SimpleArithmeticCalculator();  
background.add( calc );

JFrame frame = new JFrame(...);
frame.add( background );
frame.pack();
frame.setVisible( true );

現在計算器將顯示在背景的頂部。 或者,如果您仍然想使用 ImagePanel,那么概念仍然相同,您將計算器添加到圖像面板,將圖像面板添加到框架。

這是您的問題您在面板之前創建並顯示了 calc 以及在框架之前顯示的面板。

    calc.setVisible(true); <------------Visible first
    calc.setOpaque(false);
    panel.setVisible(true); <----Second
    panel.setOpaque(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    calc.add(panel);
    frame.add(panel);
    frame.add(calc);       
    frame.getContentPane().add(calc);   
    frame.setPreferredSize(new Dimension(358,379));
    frame.setMinimumSize(new Dimension(358,379));
    frame.setVisible(true); <---Third

然而,計算在面板上,面板在框架上。 你把它們顛倒了。 首先創建框架。 然后創建面板並將面板放在框架內。 然后制作calc並將其放入面板內。 然后顯示框架。 顯示面板。 顯示計算。 你只是順序錯了。 我已經做過很多次了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM