- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我几乎完成了这个项目,最后一件事是“隐藏”按钮。我的老师从未指导过该怎么做,但由于这让我很恼火,而且我找不到有效的答案,所以我想我应该向你们好心的人请教。
我已经尝试过:
setExtendedState(JFrame.ICONIFIED)
//导致编译器错误,找不到我的变量名setState(JFrame.ICONIFIED)
//同样的问题,“找不到符号”,或者更确切地说找到我的变量setVisible(false)
//这不起作用,因为它隐藏了我的整个框架,并且在不关闭程序的情况下我无法取回它。我使用Container c = getContentPane()
来创建 Pane ,然后在我使用的主窗口中:
ClassName variableName = new ClassName() to create the parameters.
这就是我的教学方式,我现在必须使用这种方式(因为这是我的老师想要的),但我注意到还有其他方法可以实现相同的目标。
任何特定于我的程序的输入都会很棒!谢谢!
我的程序如下(我发布了整个内容,因此不会遗漏任何内容):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Arrays;
public class Project9 extends JFrame
{
Font f1 = new Font("Serif", Font.BOLD, 30);
Font f2 = new Font("Serif", Font.PLAIN, 18);
private BOOKItem[] bookArray = new BOOKItem[10];
private JLabel headerLbl;
private JLabel messagesLbl;
private JTextField idLabelFld;
private JTextField idFld;
private JTextField priceLabelFld;
private JTextField priceFld;
private JTextField numInStockLabelFld;
private JTextField numInStockFld;
private JTextField codeLabelFld;
private JTextField codeFld;
private JTextField messagesFld;
private JButton insertBtn;
private JButton deleteBtn;
private JButton displayBtn;
private JButton displayOneBtn;
private JButton hideBtn;
private JButton clearBtn;
private String input = "";
private String displayOneStr = "";
private int idInput = 0;
private double priceInput = 0.0;
private int numInStockInput = 0;
private int codeInput = 0;
private int index = 0;
private int numItems = 0;
private int responseCode = 0;
private Container c = getContentPane();
//Main Method, sets arrayFrame params
public static void main(String[] args)
{
Project9 arrayFrame = new Project9();
arrayFrame.setSize(555,450);
arrayFrame.setVisible(true);
arrayFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Constructor
public Project9()
{
//Creates the array
for (int i = 0; i < bookArray.length; i++)
{
bookArray[i] = new BOOKItem();
System.out.println(bookArray[i]);
}
setTitle("Project 9");
c.setLayout(new FlowLayout());
headerLbl = new JLabel("Data Entry: BestBargainBook Store");
headerLbl.setFont(f1);
c.add(headerLbl);
idLabelFld = new JTextField("Enter ID:", 15);
idLabelFld.setEditable(false);
c.add(idLabelFld);
idFld = new JTextField(25);
c.add(idFld);
priceLabelFld = new JTextField("Enter Price:", 15);
priceLabelFld.setEditable(false);
c.add(priceLabelFld);
priceFld = new JTextField(25);
c.add(priceFld);
numInStockLabelFld = new JTextField("Enter Number In Stock:", 15);
numInStockLabelFld.setEditable(false);
c.add(numInStockLabelFld);
numInStockFld = new JTextField(25);
c.add(numInStockFld);
codeLabelFld = new JTextField("Enter Code: 1,2,3 or 4:", 15);
codeLabelFld.setEditable(false);
c.add(codeLabelFld);
codeFld = new JTextField(25);
c.add(codeFld);
insertBtn = new JButton("Insert");
c.add(insertBtn);
deleteBtn = new JButton("Delete");
c.add(deleteBtn);
displayBtn = new JButton("Display");
c.add(displayBtn);
displayOneBtn = new JButton("DisplayOne");
c.add(displayOneBtn);
hideBtn = new JButton("Hide");
c.add(hideBtn);
clearBtn = new JButton("Clear");
c.add(clearBtn);
messagesLbl = new JLabel("Messages:");
messagesLbl.setFont(f2);
c.add(messagesLbl);
messagesFld = new JTextField(30);
c.add(messagesFld);
//Event Listeners
insertBtn.addActionListener(new EventHandler());
deleteBtn.addActionListener(new EventHandler());
displayBtn.addActionListener(new EventHandler());
displayOneBtn.addActionListener(new EventHandler());
hideBtn.addActionListener(new EventHandler());
clearBtn.addActionListener(new EventHandler());
}//end constructor
private class EventHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == insertBtn)
{
input = idFld.getText();
idInput = Integer.parseInt(input);
input = priceFld.getText();
priceInput = Double.parseDouble(input);
input = numInStockFld.getText();
numInStockInput = Integer.parseInt(input);
input = codeFld.getText();
codeInput = Integer.parseInt(input);
insert(idInput, priceInput, numInStockInput,
codeInput);
if (responseCode == 0)
{
messagesFld.setText("Array is full. Cannot insert book ID: " +
idInput);
}
else if (responseCode == 1)
{
messagesFld.setText("Succesful insertion of " + idInput);
}
else if (responseCode == -1)
{
messagesFld.setText("Duplicate ID: " + idInput);
}
}
else if (ev.getSource() == deleteBtn)
{
input = idFld.getText();
idInput = Integer.parseInt(input);
delete(idInput);
if (responseCode == 1)
{
messagesFld.setText("Successful delete of book ID: " +
idInput);
}
else if (responseCode == -1)
{
messagesFld.setText("ID: " + idInput + " not found.");
}
}
else if (ev.getSource() == displayBtn)
{
for (index = 0; index < bookArray.length; index++)
{
bookArray[index].display();
}
}
else if (ev.getSource() == displayOneBtn)
{
input = idFld.getText();
idInput = Integer.parseInt(input);
for (index = 0; index < bookArray.length; index++)
{
if (bookArray[index].getID() == idInput)
{
bookArray[index].getID();
bookArray[index].getPrice();
bookArray[index].getNumberInStock();
bookArray[index].getCode();
messagesFld.setText("id: " + bookArray[index].getID() +
" Price: " + bookArray[index].getPrice() +
" Number In Stock: " + bookArray[index].getNumberInStock() +
" Code: " + bookArray[index].getCode());
}
}
}
else if (ev.getSource() == hideBtn)
{
}
else if (ev.getSource() == clearBtn)
{
idFld.setText("");
priceFld.setText("");
numInStockFld.setText("");
codeFld.setText("");
messagesFld.setText("");
repaint();
}
}//End actionPerformed
}//End handler
//insert method, called when insert button is pressed
public int insert(int iD, double prc, int numInStock, int code)
{
if (numItems == 10)
{
System.out.println("\nThe Array is full, please delete an entry");
responseCode = 0;
return responseCode;
}
for (index = 0; index < bookArray.length; index++)
{
if (bookArray[index].getID() == iD)
{
System.out.println("\nThat ID already exists");
responseCode = -1;
return responseCode;
}
else if (bookArray[index].getID() == 0)
{
bookArray[index] = new BOOKItem(iD, prc, numInStock, code);
numItems++;
System.out.println("\n" + idInput + "\n" + priceInput + "\n" +
numInStockInput + "\n" + codeInput + "\n" + index);
System.out.println("\nID: " + bookArray[index].getID());
System.out.println("Price: " + bookArray[index].getPrice());
System.out.println("NIS: " + bookArray[index].getNumberInStock());
System.out.println("Code: " + bookArray[index].getCode());
System.out.println("Items in Array: " + numItems);
responseCode = 1;
return responseCode;
}
}
return responseCode;
}//end insert method
//Delete method, called when delete button is pressed
public int delete(int id)
{
for (index = 0; index < bookArray.length; index++)
{
if (bookArray[index].getID() == id)
{
bookArray[index].setID(0);
bookArray[index].setPrice(0);
bookArray[index].setStock(0);
bookArray[index].setCode(0);
numItems--;
System.out.println("\nSuccessful deletion");
responseCode = 1;
return responseCode;
}
}
responseCode = -1;
return responseCode;
}//end delete method
}//end app
最佳答案
在“隐藏”按钮的 ActionListener 中,基本代码为:
JButton button = (JButton)e.getSource();
Window window = SwingUtilities.windowForComponent( button );
JFrame frame = (JFrame)window;
frame.setState(JFrame.ICONIFIED);
这样您就不会依赖于任何标识您的框架的实例变量。使用事件的源对象是使监听器通用且灵活的好方法。
关于java - 如何在主程序之外使用 JButton 来 "hide"JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178563/
我先说我是一个新手 Haskell 程序员(这些年来偶尔会修改它)但是当谈到 OOO 和命令式编程时,我有几年的时间。我目前正在学习如何使用 monad 并通过使用 monad 转换器将它们组合起来(
基本上我正在寻找的是能够从选择项的下拉列表中隐藏选项的能力。因此,从技术上讲,它们仍然是选项,但由于它们是隐藏的,因此您将无法单击它们。 我查看了文档并找到了与禁用相关的内容,不幸的是我非常希望能够隐
我正在制作一个平均堆栈应用程序,但遇到了一个问题!我尝试在第 10 个问题问完后隐藏所有内容。我使用指令 ng-hide,当我到达第 10 个问题时,该指令被设置为 true!这是代码:
好吧,我正在尝试对我发现的网站上的一项功能进行逆向工程 - 网站管理员没有回复我。有问题的网站是http://www.win-free-stuff.ca/new-contests特别是,我正在尝试构建
我正在为一个 div 设置动画。它具有以下定义: ... 我定义了以下 CSS: div.ng-hide { transition: 0.5s linear opacity; opac
我正在创建一个自定义应用程序,它将在其自己单独的 Excel 实例(新应用程序)中启动。 新创建的实例默认不可见,因此需要手动使其可见。我喜欢仅在一切设置完毕后才让我的应用程序可见 - 以避免屏幕闪烁
这是非常基本的代码: $('.myDiv').click(function() { $('.anotherDiv').hide(); alert('pause the ui');
我正在尝试为一些图片制作一个 super 简单的灯箱。基本上我有一个 div,其中包含一组使用 ng-repeat 显示的图像,我希望在单击其中一个图像时显示一个灯箱 div。我不知道为什么我的灯箱
我正在尝试为 制作动画单击按钮从左侧滑入/滑出。我正在使用 Angular 框架和 ng-show控制 显示/可见性,并将转换添加到 ng-hide样式集。 我已经成功地让 div 从左边滑入,但是
我正在写一个非常简单的幻灯片,我使用的是“常规”hide(),如下所示: jQuery("#featured li:nth-child(1)").hide('slow'); 这不仅隐藏了里,而且还慢慢
我有一个产品页面,在 Bootstrap 中以砖石格式显示 -md 和 -lg(中型和大型)屏幕的结果,其中图像可能具有不同的高度 - 对于 -sm 或 -xs,我对较小的设备使用相同高度的图像。我有
在我的活动召唤的那一刻。在其onCreate方法内部,以隐藏状态栏并以全屏模式显示。。作为向Android 30迁移的一部分,正如文档所建议的那样,我用WindowInsetsController#H
Bootstrap 显然使用了“hide”、“fade”和“in”类来进行转换。 我遇到的问题是使用“fade”和“in”会将不透明度从0更改为1。过渡效果很完美,但内容仍然占据页面上的空间,即使您看
在脱碳过程中,我现在开始使用 NSMenu 以编程方式创建菜单栏。 Carbon 似乎非常适合将标准项目添加到应用程序菜单:服务、隐藏应用程序、隐藏其他、显示全部、退出应用、甚至可以使用系统偏好设置的
我有这样的代码: Hello how are you td1 被正确隐藏,但是当 td2 被隐藏时,它
//我搜索了但没有运气,所以我开始一个新问题 :) 我有: Notification 我想要:当我点击这个 a ,它将显示/隐藏一个 div,当我在该 div 外部单击时,如果它可见,它就会隐藏。 我
Test App.js: (function() { angu
想要基于下拉(选择)隐藏或显示 AngularJS 上的元素,但由于该页面是与 QlikSense 的混搭,因此来自 Qlik 的元素不能与 ng-show 很好地配合。我用 ng-show 尝试了这
如何使下面的代码更简单、更少? 谢谢。 012 012 $('.btn div:eq(0)').click(function(){ $('.content div').hide();
我看到一些方法可以在 jQuery UI 的对话框中隐藏标题栏和标题栏中的图标,但在 jQuery Mobile 中没有。有谁知道如何在 jQuery Mobile 中做到这一点? 从对话框中删除/隐
我是一名优秀的程序员,十分优秀!