计算器:仅当鼠标悬停在测试 JFrame 类中的按钮上时才会显示按钮

2024-03-23

我很着急,所以我想知道重复的事情。我仍在努力学习 Java 和术语,直到本学期结束。我用了一个模板。我正在使用背景图像(“面板”),这使一切变得复杂。

基本上,这些按钮仅当我将鼠标悬停在它们上方时才会显示。显然,它与 JPanel 有关。

我排除了您可能会要求的代码,希望这次有人帮助我,因为我的按钮与我在查看其他推荐帖子时看到的按钮不同。

另外,我可以将 JFrame 设置为固定大小(测试类代码中的大小)吗?

代码可能是多余的,但我只是想让一切正常工作。请记住,我是 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);
}
}

MAIN:

    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);

Update:

另一个问题可能是以下代码:

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

默认情况下,框架使用 BorderLayout。当您将组件添加到框架时,默认情况下它们会添加到中心,问题是一次只能将一个组件添加到中心。看起来您正在尝试拥有背景图像。如果是这种情况,那么正如我之前建议的,您可以使用 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,那么概念仍然相同,您将计算器添加到图像面板,并将图像面板添加到框架。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

计算器:仅当鼠标悬停在测试 JFrame 类中的按钮上时才会显示按钮 的相关文章

随机推荐

  • 我如何退出我的应用程序?

    我如何退出我的应用程序 我希望当用户单击按钮时 该应用程序将完全退出 我看到了任何有关它的答案 但他们将该应用程序带到了后端 我想彻底退出 在这个方法中你可以调用exit 在任何地方运行并退出应用程序 所以要随时关闭应用程序 请使用FLAG
  • 查找目录中的文件数

    Linux 中是否有任何方法可以在 O 1 内计算目录 即直接子级 中的文件数 与文件数无关 而不必先列出目录 如果不是 O 1 是否有一种相当有效的方法 我正在寻找替代方案ls wc l readdir 并不像您想象的那么昂贵 诀窍是避免
  • C#:如何使用 directshow.net 显示此对话框?

    如何使用 Directshow net 调用此对话框 过滤图 https stackoverflow com questions 4680606 c how to open configuration pin dialog 假设您有 IBa
  • C语言中如何释放内存?

    我正在编写具有大量一维和二维数组的代码 我收到 错误 无法分配区域 我认为这是因为分配了太多内存 我使用 malloc 和 free 函数 但我不确定我是否正确使用它们 也许你知道我在哪里可以看到关于 C 内存管理的好例子 所以 我只是想让
  • 没有可用于离线模式的缓存版本 Gradle 插件

    我有一段时间没有使用 Android Studio 但决定更新所有内容并创建一个新项目 如果我转到 首选项 gt 构建 执行 部署 gt Gradle 我会收到上述错误 没有像其他地方建议的那样 离线工作 选项没有可用于离线模式的 grad
  • 绑定 IList 不显示 IMyInterface 继承的接口成员

    我将 IList 绑定到 GridView IMyInterface 看起来像 public interface IMyInterface IHasTotalHours IHasLines DateTime GoalStartDate ge
  • SQL Server 列的层次总和

    我按照图表设计了数据库 Category表是自引用父子关系 Budget将为每个类别定义所有类别和金额 Expense表将包含已花费金额的类别条目 考虑Total此表中的列 我想编写选择语句来检索具有以下给出的列的数据集 ID Catego
  • 将 ActionBar 添加到 Android API Level 8 应用程序?

    我知道互联网上有很多关于如何做到这一点的零碎材料 但我请求有人给我一个逐步指南 告诉我如何从一开始就如何添加库 将代码添加到我的 Android 应用程序 项目 我喜欢这个的外观 https github com johannilsson
  • 如何通过docker API正确获取docker镜像的总大小?

    我想通过docker API获取docker镜像的总大小 例如GET v2
  • 在 Laravel 中转义带引号的字符串

    我想将 Excel 文件的内容插入到我的数据库中 我只是使用原始查询来实现此目的 The controller function public function uploadExcel filename Input file import
  • jetpack compose 中的 textAllCaps

    我怎样才能达到同样的效果textAllCaps在 Jetpack Compose 中 我知道我可以使用toUpperCase字符串本身的方法将字符串转换为大写 但我想知道是否有一个属性可以添加到Text可组合以直观地将文本转换为大写 Tex
  • 如何使用关键字跳过机器人框架中的测试用例

    我正在尝试使用关键字跳过特定的测试用例 是否有任何关键字可以做到这一点 我想做的是检查文件名是否有 跳过 一词 然后我想跳过它 是否有任何关键字 例如 跳过测试 跳过执行如果 regex Get Regexp Matches TEST NA
  • Laravel 身份验证 - 不同表中的电子邮件

    我有两张桌子 persons id name email phone person type id users id person id password role id etc 你能告诉我如何让 Laravel 5 8 内置的身份验证系统
  • PDF不显示图像:html2pdf库

    我使用 Angular 5 和 html2pdf 库来帮助创建 pdf 文件 https github com eKoopmans html2pdf https github com eKoopmans html2pdf 这用于我的 Ang
  • 具有自定义模型的实体框架核心原始 SQLQueries

    使用 Entity Framework 6 我能够执行原始 SQL 查询并使用 DBContext 中未定义的自定义模型来存储查询的输出 一个简单的例子如下 List
  • Jupyter 小部件未出现在笔记本中

    我正在运行 jupyter 笔记本 但 jupyter 小部件没有出现 相反 我收到以下消息 Failed to display Jupyter Widget of type Button If you re reading this me
  • Java - 使用流转置列表的列表

    为了改进目的 我尝试专门使用流来转置列表列表 我的意思是 我有一个双精度列表列表 其中包含例如 1 2 3 4 5 6 7 8 我想获得一个包含双打列表的列表 1 5 2 6 3 7 4 8 以下 Stack Overflow 问题提供了一
  • .designer 文件未与 Visual Studio 中的 .cs 文件关联?

    EDIT 似乎有一种视觉效果 视觉工作室中的错误 当我将网站文件夹作为网站打开并查看 Views ascx designer cs 时 它没有显示它是关联的 但是 如果我打开同一网站的解决方案文件 那么这些文件就会关联并且一切正常 需要明确
  • IIS VS 2008 / Web.config - 日期格式错误

    谁能帮忙 我最近移动了服务器 它的 IIS7 而不是我们之前的 IIS6 但其他一切都是一样的 我已将区域设置为英国 所有日期格式加上系统本地 我的意思是一切 但是当网页 asp net 在 IIS7 上运行时 它认为日期是另一种格式 这真
  • 计算器:仅当鼠标悬停在测试 JFrame 类中的按钮上时才会显示按钮

    我很着急 所以我想知道重复的事情 我仍在努力学习 Java 和术语 直到本学期结束 我用了一个模板 我正在使用背景图像 面板 这使一切变得复杂 基本上 这些按钮仅当我将鼠标悬停在它们上方时才会显示 显然 它与 JPanel 有关 我排除了您