- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这就是我使用 previous question 中教给我的 actionlistener
所做的事情。 。我首先要为我的代码很长表示歉意。我已经稍微修剪了一下。
package testpanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class TestPanel {
JFrame Card = new JFrame();
GridBagConstraints c = new GridBagConstraints();
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
JButton up;
JButton up_1; JButton up_2; JButton up_3; JButton up_4;
JButton up_5; JButton up_6; JButton up_7; JButton up_8;
JButton down;
JButton down_1; JButton down_2; JButton down_3; JButton down_4;
JButton down_5; JButton down_6; JButton down_7; JButton down_8;
JTextField number;
JTextField number_1; JTextField number_2; JTextField number_3;
JTextField number_4; JTextField number_5; JTextField number_6;
JTextField number_7; JTextField number_8;
int value1000 = 0;
int value500 = 0;
int value200 = 0;
int value100 = 0;
int value50 = 0;
int value20 = 0;
int value10 = 0;
int value5 = 0;
int value1 = 0;
int valuecoins = 0;
ActionListener upaction;
ActionListener downaction;
public TestPanel(){
Card.setVisible(true);
Card.setSize(522,500);
Card.setTitle("Frequency Distribution");
Card.setResizable(false);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/4);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);
Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel centerpanel = new JPanel();
centerpanel.setBorder(etch);
centerpanel.setPreferredSize(new Dimension(0,324));
centerpanel.setBackground(new Color(0,120,0));
centerpanel.setLayout(new GridBagLayout());
JLabel lbl1000 = new JLabel("1,000.00");
lbl1000.setFont(new Font("Calibri", Font.BOLD, 20));
lbl1000.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl1000,c);
JLabel multiply = new JLabel("X");
multiply.setFont(new Font("Calibri", Font.BOLD, 20));
multiply.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply,c);
final JTextField number = new JTextField();
number.setText(value100+"");
number.setColumns(3);
number.setFont(new Font("Calibri", Font.BOLD, 20));
number.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number,c);
upaction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == up){
value100++;
if(value100>0)
{
number.setText(value100+"");
down.setEnabled(true);
}}
if(e.getSource() == up_1){
value500++;
if(value500>0)
{
number_1.setText(value500+"");
down_1.setEnabled(true);
}}
if(e.getSource() == up_2){
value200++;
if(value200>0)
{
number_2.setText(value200+"");
down_2.setEnabled(true);
}}
if(e.getSource() == up_3){
value100++;
if(value100>0)
{
number_3.setText(value100+"");
down_3.setEnabled(true);
}}
if(e.getSource() == up_4){
value50++;
if(value50>0)
{
number_4.setText(value50+"");
down_4.setEnabled(true);
}}
}
};
downaction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == down){
value1000--;
if(value1000>0)
{number.setText(value1000+"");}
if(value1000==0)
{
number.setText(value1000+"");
down.setEnabled(false);
value1000=0;
}}
if(e.getSource() == down_1){
value500--;
if(value500>0)
{number_1.setText(value500+"");}
if(value500==0)
{
number_1.setText(value500+"");
down_1.setEnabled(false);
value500=0;
}}
if(e.getSource() == down_2){
value200--;
if(value200>0)
{number_2.setText(value200+"");}
if(value200==0)
{
number_2.setText(value200+"");
down_2.setEnabled(false);
value200=0;
}}
if(e.getSource() == down_3){
value100--;
if(value100>0)
{number_3.setText(value100+"");}
if(value100==0)
{
number_3.setText(value100+"");
down_3.setEnabled(false);
value100=0;
}}
if(e.getSource() == down_4){
value50--;
if(value50>0)
{number_4.setText(value50+"");}
if(value50==0)
{
number_4.setText(value50+"");
down_4.setEnabled(false);
value50=0;
}}
}
};
up = new JButton("up");
up.setBackground(new Color(224,223,227));
up.setPreferredSize(new Dimension(5,15));
up.addActionListener(upaction);
down = new JButton("down");
down.setBackground(new Color(224,223,227));
down.setPreferredSize(new Dimension(5,15));
down.setEnabled(false);
down.addActionListener(downaction);
JPanel updownpanel = new JPanel();
updownpanel.setPreferredSize(new Dimension(10,10));
updownpanel.setLayout(new BorderLayout());
updownpanel.add(up, BorderLayout.NORTH);
updownpanel.add(down, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel,c);
JLabel equals = new JLabel("=");
equals.setFont(new Font("Calibri", Font.BOLD, 20));
equals.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 0;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals,c);
JLabel total = new JLabel();
total.setText(value100+".00");
total.setFont(new Font("Calibri", Font.BOLD, 20));
total.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 0;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total,c);
//-----------------------------------------------------------------------------------------
JLabel lbl500 = new JLabel("500.00");
lbl500.setFont(new Font("Calibri", Font.BOLD, 20));
lbl500.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl500,c);
JLabel multiply_1 = new JLabel("X");
multiply_1.setFont(new Font("Calibri", Font.BOLD, 20));
multiply_1.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply_1,c);
number_1 = new JTextField();
number_1.setText(value500+"");
number_1.setColumns(3);
number_1.setFont(new Font("Calibri", Font.BOLD, 20));
number_1.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 1;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number_1,c);
up_1 = new JButton("up");
up_1.setBackground(new Color(224,223,227));
up_1.setPreferredSize(new Dimension(5,15));
up_1.addActionListener(upaction);
down_1 = new JButton("down");
down_1.setBackground(new Color(224,223,227));
down_1.setPreferredSize(new Dimension(5,15));
down_1.setEnabled(false);
down_1.addActionListener(downaction);
JPanel updownpanel_1 = new JPanel();
updownpanel_1.setPreferredSize(new Dimension(10,10));
updownpanel_1.setLayout(new BorderLayout());
updownpanel_1.add(up_1, BorderLayout.NORTH);
updownpanel_1.add(down_1, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 1;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel_1,c);
JLabel equals_1 = new JLabel("=");
equals_1.setFont(new Font("Calibri", Font.BOLD, 20));
equals_1.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 1;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals_1,c);
JLabel total_1 = new JLabel();
total_1.setText(value500+".00");
total_1.setFont(new Font("Calibri", Font.BOLD, 20));
total_1.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 1;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total_1,c);
//-----------------------------------------------------------------------------------------
JLabel lbl200 = new JLabel("200.00");
lbl200.setFont(new Font("Calibri", Font.BOLD, 20));
lbl200.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl200,c);
JLabel multiply_2 = new JLabel("X");
multiply_2.setFont(new Font("Calibri", Font.BOLD, 20));
multiply_2.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply_2,c);
number_2 = new JTextField();
number_2.setText(value200+"");
number_2.setColumns(3);
number_2.setFont(new Font("Calibri", Font.BOLD, 20));
number_2.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 2;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number_2,c);
up_2 = new JButton("up");
up_2.setBackground(new Color(224,223,227));
up_2.setPreferredSize(new Dimension(5,15));
up_2.addActionListener(upaction);
down_2 = new JButton("down");
down_2.setBackground(new Color(224,223,227));
down_2.setPreferredSize(new Dimension(5,15));
down_2.setEnabled(false);
down_2.addActionListener(downaction);
JPanel updownpanel_2 = new JPanel();
updownpanel_2.setPreferredSize(new Dimension(10,10));
updownpanel_2.setLayout(new BorderLayout());
updownpanel_2.add(up_2, BorderLayout.NORTH);
updownpanel_2.add(down_2, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 2;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel_2,c);
JLabel equals_2 = new JLabel("=");
equals_2.setFont(new Font("Calibri", Font.BOLD, 20));
equals_2.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 2;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals_2,c);
JLabel total_2 = new JLabel();
total_2.setText(value200+".00");
total_2.setFont(new Font("Calibri", Font.BOLD, 20));
total_2.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 2;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total_2,c);
//-----------------------------------------------------------------------------------------
JLabel lbl100 = new JLabel("100.00");
lbl100.setFont(new Font("Calibri", Font.BOLD, 20));
lbl100.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 3;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl100,c);
JLabel multiply_3 = new JLabel("X");
multiply_3.setFont(new Font("Calibri", Font.BOLD, 20));
multiply_3.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply_3,c);
number_3 = new JTextField();
number_3.setText(value100+"");
number_3.setColumns(3);
number_3.setFont(new Font("Calibri", Font.BOLD, 20));
number_3.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 3;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number_3,c);
up_3 = new JButton("up");
up_3.setBackground(new Color(224,223,227));
up_3.setPreferredSize(new Dimension(5,15));
up_3.addActionListener(upaction);
down_3 = new JButton("down");
down_3.setBackground(new Color(224,223,227));
down_3.setPreferredSize(new Dimension(5,15));
down_3.setEnabled(false);
down_3.addActionListener(downaction);
JPanel updownpanel_3 = new JPanel();
updownpanel_3.setPreferredSize(new Dimension(10,10));
updownpanel_3.setLayout(new BorderLayout());
updownpanel_3.add(up_3, BorderLayout.NORTH);
updownpanel_3.add(down_3, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 3;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel_3,c);
JLabel equals_3 = new JLabel("=");
equals_3.setFont(new Font("Calibri", Font.BOLD, 20));
equals_3.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 3;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals_3,c);
JLabel total_3 = new JLabel();
total_3.setText(value100+".00");
total_3.setFont(new Font("Calibri", Font.BOLD, 20));
total_3.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 3;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total_3,c);
JLabel coins = new JLabel("Coins:");
coins.setFont(new Font("Calibri", Font.BOLD, 20));
coins.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 9;
c.insets = new Insets(0,0,0,0);
centerpanel.add(coins,c);
JTextField txttotal = new JTextField();
txttotal.setText(valuecoins+"");
txttotal.setColumns(6);
txttotal.setFont(new Font("Calibri", Font.BOLD, 20));
txttotal.setForeground(Color.BLACK);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 9;
c.insets = new Insets(0,0,0,35);
centerpanel.add(txttotal,c);
JLabel divider = new JLabel("---------------------");
divider.setFont(new Font("Calibri", Font.BOLD, 20));
divider.setForeground(Color.GRAY);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 10;
c.insets = new Insets(0,0,0,0);
centerpanel.add(divider,c);
JLabel txtCoinTotal = new JLabel();
txtCoinTotal.setText(valuecoins+".00");
txtCoinTotal.setFont(new Font("Calibri", Font.BOLD, 20));
txtCoinTotal.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 11;
c.insets = new Insets(0,0,0,35);
centerpanel.add(txtCoinTotal,c);
Card.add(centerpanel);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestPanel test = new TestPanel();
}
});
}
}
我这里有 4 行数字,其中有一个 textfield
和一个用于递增的向上和向下按钮。它按照我喜欢的方式工作,但是如果您运行程序并从上到下单击向上按钮一次,则第 4 行将是 2 而不是 1。即使您添加行,第 4 行始终会获得 2 而不是 1。如果您从底部开始单击,第一行将为 2。如果不是第四行,则第一个获得 2。我没有发现我的代码有任何问题,但就是这样。即使我正确输入了代码,第一行的后退按钮也不起作用。我已经检查了我的代码两次以上,但我没有看到任何可以使它成为这样的东西。
最佳答案
如果我们比较 up
操作的代码...
if (e.getSource() == up) {
value100++;
if (value100 > 0) {
number.setText(value100 + "");
down.setEnabled(true);
}
}
//...
if (e.getSource() == up_3) {
value100++;
if (value100 > 0) {
number_3.setText(value100 + "");
down_3.setEnabled(true);
}
}
使用向下
操作的代码
if (e.getSource() == down) {
value1000--;
if (value1000 > 0) {
number.setText(value1000 + "");
}
if (value1000 == 0) {
number.setText(value1000 + "");
down.setEnabled(false);
value1000 = 0;
}
}
//...
if (e.getSource() == down_3) {
value100--;
if (value100 > 0) {
number_3.setText(value100 + "");
}
if (value100 == 0) {
number_3.setText(value100 + "");
down_3.setEnabled(false);
value100 = 0;
}
}
我们可以看到差异...up
正在修改value100
,而down
正在修改value1000
,但 up_3
和 down_3
正在修改 value100
...
趁我还有时间...
这就是您的 UI 在我的电脑上的样子...
现在,我坐在这里挠头“......”
现在,如果我用 pack
替换 setSize
(在构造函数末尾调用),我会得到...
现在,如果我删除所有 setPreferredSize
调用,我会得到...
仍然需要一些工作,但这是一个更好的结果(恕我直言)......
看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?关于该主题的决定...
哦,我不知道这是否重要,但你可能想看看 How to use spinners
关于java - 如何修复 Action 监听器输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993255/
我将一个 div 设置为 100% 宽度,当以 1024 分辨率查看页面时,宽度应从 100% 变为 1000px,我让它与@media 查询一起正常工作,并且在 FF、safari chrome 上
希望有人能帮助我,我已经被困了几天了。 将我的 Domino 服务器更新到 9.01 Fix 3 后,我在 javascript 控制台上不断收到错误消息: TypeError: this.edito
我们正在使用一个基于RMI的java应用程序。当我们运行应用程序时,即使应用程序处于理想阶段,内存使用量仍然不断增加。我们主要使用Vector和散列图数据结构。如何最大限度地减少java内存使用/修复
概述 Internet Download Manager (IDM)是最流行的 Windows 下载管理器。如果你平时工作中使用过IDM,您会惊叹 IDM 下载文件的速度有多快。IDM
当我打开 brave 浏览器时,会打开一个窗口(如下所示)。它并没有真正干扰浏览器的处理。但令人担忧的是为什么这种情况一直发生...... Error On Opening Brave Browser
这是我今天在求职面试中被问到的一个问题: 看下面的代码: int n=20; for (int i =0; i
我不小心删除了/opt/local/bin/perl5.8.9 ,这似乎是 macports 编译的 perl 的主要二进制文件。 现在我有很多取决于 perl5 的端口,但不想卸载并重新安装所有端口
>>>flip fix (0 :: Int) (\a b -> putStrLn "abc") Output: "abc" 这是使用翻转修复的简化版本。 我在一些 YouTube 视频中看到了这种使用
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (64 个回答) 已关闭 3 年前。 我在 Android 应
def main(): cash = float(input("How much money: ")) coins = 0 def changeCounter(n): whil
前一周我遇到了类似的问题,查询需要永远运行。在编写此查询时,我尝试应用从其他查询中学到的一些知识,但执行起来需要很长时间。 运行查询的两个单独部分时,每个部分需要 2 分钟才能完成,这是可以接受的,但
下午,我的 CSS 有问题。第三个下拉菜单放错了,我没有解决办法。 这是我想要的: 之前: http://i53.tinypic.com/2qu85z8.png 之后: http://i51.tiny
更新方法: override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingS
我知道这是一个很多人都遇到过的问题,但我不熟悉 Less 并且是 Bootstrap 的新手,我正在寻找一种全 CSS 解决方案来防止我的导航栏折叠到 768 像素以下:
在我的布局中,我创建了以下 jsfiddle 托管的可调整大小的粘性页脚。但是,在调整大小时它与内容重叠。有没有办法让它在所有浏览器上都能响应? http://jsfiddle.net/9aLc0mg
我想要实现的目标 racer-offset 是为了让用户可以设置图像可以以 px 为单位移动多远。偏移量管理偏移量。 Speed-racer 告诉我们图像在滚动过程中移动的速度。我的问题是它不会停止。
我有一个简单的自动换行函数,它接受一个长字符串作为输入,然后将该字符串分成更小的字符串,并将它们添加到一个数组中,以便稍后输出。现在最后一两个字没有输出。这是主要问题。但是,我还想改进功能。我知道这有
我试图在使用每个 slider 之前禁用“下一步”按钮,我不确定为什么在单击不再是 class="not-clicked"的同一个 slider 时取消禁用该按钮. JSFiddle: (这里看起来有
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 8 年前。 如何让程序输出所有信息? IT
On this page ,在“生活”下有一个带有自动生成的子菜单的菜单。子菜单存在一些问题(它会闪烁并改变大小——如果你滚动它就会看到)。我需要以某种方式覆盖它当前正在读取的 css 并使其统一。
我是一名优秀的程序员,十分优秀!