在JAVA GUI中执行的动作代码中,我如何计算按下按钮的次数,并且每次按下按钮时都会执行不同的操作?
private class Listener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
HOW WOULD I COUNT HOW MANY TIMES THIS BUTTON HAS BEEN PRESSED?
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!!
嗨,我想把我的按钮放在南方位置!我怎样才能做到这一点?这是我的代码:
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;
public class TableDemo extends JPanel {
private static Icon leftButtonIcon;
private boolean DEBUG = false;
// added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();
public TableDemo() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JButton button=new JButton("Buy it");
button.setSize(30,60);
button.add(button);
//Create the scroll pane and …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Eclipse创建Java 7地址簿,当我测试运行JFrame时,JLabel不可见并且JButton重叠.(按钮将占用整个JFrame)例如
add(saveButton);
add(cancelButton);
add(headingLabel);
headingLabel.setVisible(true);
cancelButton.setLocation(200,200);
saveButton.setLocation(400,200);
cancelButton.setSize(200,50);
saveButton.setSize(200,50);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在制作游戏Master Mind,我已经用JButton填充了我的矩阵,所以人们可以点击它们来改变颜色.
现在我想将矩形按钮的形状更改为圆形,是否有一种方法可以一次更改它们,因为我使用循环来创建它们.
我在获取按钮的ID /变量名时遇到问题.一般来说,我想创造出游戏灯.我创建了按钮面板(NxN,N来自组合框),但我不知道如何在按下后获得单个按钮的ID.任何的想法?
private void p1ActionPerformed(java.awt.event.ActionEvent evt) {
String temp = jComboBox1.getSelectedItem().toString();
ile=Integer.parseInt(temp);
jPanel1.removeAll();
jPanel1.setLayout(new GridLayout(ile,ile));
for (int i = 0; i < ile; i++) {
for (int j = 0; j < ile; j++) {
JButton btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
btn.setBackground(Color.green);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
/*What put here?*/
}
});
jPanel1.add(btn);
revalidate();
pack();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我希望在应用程序前面显示警告showConfirmDialog窗口,即使GUI移动到不同的位置,如果我不移动应用程序并按"关闭ALT + X"按钮,但是如果我将应用程序移动到第二个屏幕,它可以正常工作警告showConfirmDialog窗口停留在旧位置,如何随GUI一起移动警告窗口,请给我指示,谢谢.
关闭ALT + X按钮
//close window button
JButton btnCloseWindow = new JButton("Close ALT+X");
btnCloseWindow.setMnemonic('x');
btnCloseWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame();
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
//find the position of GUI and set the value
//dialog.setLocation(10, 20);
if (result == JOptionPane.YES_OPTION)
System.exit(0);
}
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,我试图将GUI的位置设置为showConfirmDialog,但是没有用.
我正在为Java中的类创建一个最小的绘制应用程序.我需要为用户制作几个按钮来选择不同的形状,在这些按钮上我应该放置他们正在使用的形状的图像.例如,允许用户绘制线条的按钮应该在其上具有线条的图像.一个绘制矩形的按钮,应该是一个矩形.我需要能够在程序中执行此操作,而无需使用外部图像源.
这是我当前的按钮代码示例.
lineB = new JButton();
lineB.setBounds(0, 25, 20, 20);
lineB.setBackground(Color.WHITE);
shapePanel.add(lineB);
lineB.addActionListener(this);
Run Code Online (Sandbox Code Playgroud) 这是我正在尝试做的事情:创建一个JButton,当点击时,setText()它将变为Happy,然后再次点击时,setText()它将变为Sad.基本上,我希望它在点击之间Happy和之间切换Sad.(抱歉可怕的解释)
问题是该按钮仅在我第一次单击它时(从Happy到Sad)更改文本.再次单击时,它不会更改文本.
这是我的切换方法的代码:
boolean toggleButton = true;
JButton button = new JButton("Sad");
public void changeButtonText() {
if(toggleButton = true) {
toggleButton = false;
button.setText("Happy");
} else if (toggleButton = false) {
toggleButton = true;
button.setText("Sad");
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我有我的ActionListener:
class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
changeButtonText();
}
}
button.addActionListener (new ButtonListener());
Run Code Online (Sandbox Code Playgroud) 我想设置一个JButton,使它的图标对齐到它的左边,而文本居中.
我已经找到了如何让他们中的一个离开,另一个,或两者在同一设置,但我找不到我要找的东西.
当然,我总是可以重新定义油漆方法,但我正在寻找一种更精简的方法.