标签: jbutton

向 JDialog 添加按钮?

我正在尝试在 JDialog 上制作 JButton,但是该按钮将覆盖整个 JDialog,对此有什么帮助吗?它看起来是这样的:

在此输入图像描述

这就是我创建 JDialog 和 JButton 的方法:

class MenuStoreHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) dim.getWidth();
        int screenHeight = (int) dim.getHeight();

        JDialog g = new JDialog();
        g.setTitle("The Store");
        g.setSize(200, 200);
        g.setLocation(screenWidth / 2 - 150, screenHeight / 2 - 150);

        JButton b = new JButton("Buy");
        b.addActionListener( new StoreItem1Handler() );
        b.setVisible(true);
        g.add(b);

        g.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

我将发布完整的 MrStan.class,如下:

package Progress;

public class MrStan extends JPanel{

    private Timer timer = new Timer(); …
Run Code Online (Sandbox Code Playgroud)

java swing jdialog jbutton

1
推荐指数
1
解决办法
1万
查看次数

使用swing调用父窗体的方法

我有一个 JFrame 表单StaffListMain,它在按钮单击事件之一中具有以下代码:

private void btnManageLeaveActionPerformed(java.awt.event.ActionEvent evt) {
    // Open the new form and pass the selected staff member
    ManageLeave manageLeaveForm = new ManageLeave(staff.getStaffAt(lstStaff.getSelectedIndex()));
    manageLeaveForm.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)

StaffListMain类也有一个方法调用writeToFile(),一个我想其他类内使用,如在一个上面的代码段(ManageLeaveForm)。

因此,我需要一种方法来调用另一种形式的方法。这是可能的,还是我必须writeToFile()分成另一个类,然后根据需要在每个单独的类中使用它?

java swing constructor jbutton actionlistener

1
推荐指数
1
解决办法
691
查看次数

Swing - 使用getComponent()更新所有JButton

我正在制作一个tictactoe游戏,其中每个棋盘片由JButton代表.当有人单击该按钮时,文本将更改为"X"或"O".我正在写一个重置功能,它将所有按钮中的文本重置为"".我使用getComponents()方法从数组访问所有按钮.

我只是想知道我做错了什么因为这个位编译正确

component[i].setEnabled(true);
Run Code Online (Sandbox Code Playgroud)

但这一点没有

component[i].setText("");
Run Code Online (Sandbox Code Playgroud)

我收到"找不到符号"错误.请看下面的代码.我只包括我认为必要的代码.

    JPanel board = new JPanel(new GridLayout(3, 3));

    JButton button1 = new JButton("");
    JButton button2 = new JButton("");
    JButton button3 = new JButton("");
    JButton button4 = new JButton("");
    JButton button5 = new JButton("");
    JButton button6 = new JButton("");
    JButton button7 = new JButton("");
    JButton button8 = new JButton("");
    JButton button9 = new JButton("");

    board.add(button1);
    board.add(button2);
    board.add(button3);
    board.add(button4);
    board.add(button5);
    board.add(button6);
    board.add(button7);
    board.add(button8);
    board.add(button9);

public void reset()
{
    Component[] component = board.getComponents();

    // Reset user interface
    for(int …
Run Code Online (Sandbox Code Playgroud)

java swing jbutton

1
推荐指数
1
解决办法
1万
查看次数

从 JFrame 类继承到另一个类并使用变量

我是 Java 新手,正在开发一个使用 GUI 的程序。我的问题是我已经达到了1000 行,这很有压力,因为我所有的代码都在一个文件中。我不喜欢这个。所以我在互联网上研究了继承。但是,我不确定这如何帮助我解决我的问题,因为我所知道的所有继承都继承了父类中的所有内容。但是,当我尝试在子类中使用变量时,我不能说我在父类中有这样的东西:

JButton getButton = new JButton("Enter");

现在当我去儿童班时。我想继承并使用这个变量,所以我可以使用它不工作的 ActionListener。

那么有人可以告诉我我在这里做错了什么,或者这不是正确的方法吗?

前后谢谢大家。

这是我的一些代码:

    public class H_Store extends JFrame {

    private JCheckBox groundMeatCheckBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    H_Store frame = new H_Store();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public H_Store() {
        super("Hussin Store");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1378, 657); …
Run Code Online (Sandbox Code Playgroud)

java inheritance swing jbutton actionlistener

1
推荐指数
1
解决办法
3914
查看次数

有没有办法在 JButton 中存储数据?

我的程序当前将一个图像添加到一个列表中,然后它会创建一个动态存储缩略图的 Jbutton。喜欢在这里找到。尽管图像是在用户选择时添加的。

我遇到的问题是我无法将图像更改为所选的缩略图,因为它们未链接到列表中的图像。有没有办法可以将图像的索引号存储在按钮中,所以当我单击它时,我知道要在列表中显示哪个图像?或者还有其他更智能的方法吗?谢谢。

 //Create thumbnail
    private void createThumbnail(ImpImage image){
        Algorithms a = new Algorithms();
        ImpImage thumb = new ImpImage();
        //Create Thumbnail
        thumb.setImg(a.shrinkImage(image.getImg(), 75, 75));
        //Create ImageIcon
        ImageIcon icon = new ImageIcon(thumb.getImg());
        //Create JButton
        JButton iconButton = new JButton(icon); 
        //Create ActionListener
        iconButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bottomBarLabel.setText("Clicked");
                imagePanel.removeAll();
                imagePanel.add(images.get(position)); //Needs Fixing
                imagePanel.revalidate();
            }
        });
        //Add to previewPanel
        previewPanel.add(iconButton);
        previewPanel.revalidate();
        previewPanel.repaint();
    }
Run Code Online (Sandbox Code Playgroud)

我实现的方式是:1)打开图像,2)将图像添加到列表,3)创建图像的缩略图 -> 添加到 ImageIcon -> 添加到 Jbutton -> 添加到带有 actionListener 的组件。问题是我没有将按钮存储在列表中,因此它不知道其各自的图像在列表中的编号。

java image dynamic jbutton imageicon

1
推荐指数
1
解决办法
3709
查看次数

尝试将ActionListener添加到JButtons

我无法弄清楚如何添加ActionlistenersJButtons,任何帮助将非常感激.

public class Translator extends JPanel implements MouseListener, ActionListener {       

    private JButton french = new JButton();
    private JButton german = new JButton();
    private JButton irish = new JButton();

    public Translator(){
        french = new JButton("French");
        german = new JButton("German");
        irish = new JButton("Irish");           
        setLayout(new GridLayout(2,1));         
        buttonPanel.setLayout(new GridLayout(1,3));
        buttonPanel.add(french);
        buttonPanel.add(german);
        buttonPanel.add(irish);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

java swing jbutton actionlistener

1
推荐指数
1
解决办法
1308
查看次数

单击后删除按钮?

我如何让我的程序工作,以便在单击按钮时将其删除?

这是代码:

//Mainmenu
JFrame frame1 = new JFrame();
Container pane = frame1.getContentPane();

JButton a = new JButton(new ImageIcon("path2img"));
BufferedImage a1 = ImageIO.read(new File("path2img"));

 public Menu() throws IOException {
     frame1.setSize(300, 450);
    frame1.setLocationRelativeTo(null);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setResizable(false);
    frame1.setVisible(true);
    pane.add(a);
    a.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent aa) {
            pane.remove(a);

        }


    });
            }
Run Code Online (Sandbox Code Playgroud)

谢谢

java action jframe jbutton

1
推荐指数
1
解决办法
2108
查看次数

如何制作 JButton 的 for 循环?

public class BoardView extends JFrame
 {

private JButton board [][];

private GameBoard gameboard;

public static JButton cat1 = new JButton();
public static JButton cat2 = new JButton();
public static JButton car1 = new JButton();
public static JButton car2 = new JButton();
public static JButton dog1 = new JButton();
public static JButton dog2 = new JButton();
public static JButton bike1 = new JButton();
public static JButton bike2 = new JButton();

public BoardView()
{
    Container buttonLayout;
    /**
     * Exits the program when …
Run Code Online (Sandbox Code Playgroud)

java jbutton

1
推荐指数
1
解决办法
1万
查看次数

单击按钮时创建不同的 jLabel

我编写了我的第一个 Java 应用程序:一个杂货清单。我的第一个版本运行良好。如果您填写两个文本字段并从下拉列表中选择一个项目并按“添加”按钮。带有金额和单位的项目显示在第二个 jPanel 的文本区域中。但现在我不想将其显示在文本区域中,而是希望将项目显示在 jLabels 中。因此,每次单击“添加”按钮时,第二个 jPanel 中都会出现一个新标签。但我似乎无法让它发挥作用。我在 stackoverflow 和 google 上的不同网站上阅读了很多不同的方法,我也阅读了 oracle 网站。但我一定做错了什么。你能看一下我的代码并告诉我我做错了什么吗?

代码

GroceryList2ActionListener.java

package javaclasses;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import grocerylist2jframe.GroceryList2JFrame;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JLabel;

public class GroceryList2ActionListener {

    public static class GroceryListActionListenerHandler extends GroceryList2JFrame   {
        protected static String groceryItem, unit, quantity;
        protected static JButton buttonAddGroceryItemToGrocerylist = buttonAddGroceryItem, buttonRemoveGroceryItemFromGrocerylist = buttonRemoveGroceryItem;
        protected static JLabel label;

        public static void getButtonActionAddGroceryItem() {
            buttonAddGroceryItemToGrocerylist.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    groceryItem = jTextField1GroceryItem.getText(); …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel jbutton

1
推荐指数
1
解决办法
1049
查看次数

创建一个JButton数组

基本上我正在尝试制作一个Lightout游戏!我想创建一个JButton数组,以便我可以跟踪每个按钮的索引(原因是每个按钮的状态取决于其他按钮的状态)

到目前为止我有:

JPanel panel = new JPanel();
    setTitle("Memory");
    setContentPane(panel);
    setPreferredSize(new Dimension(300, 300));
    panel.setLayout(new GridLayout(5,5));


    JButton[][] buttons = new JButton[5][5] ;
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++) {
          buttons[i][j] = new JButton();

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
        }
Run Code Online (Sandbox Code Playgroud)

但这并不像我期望的那样.当我运行它时,我刚刚得到了一个空白的JFrame.任何帮助,将不胜感激

java arrays swing jbutton

1
推荐指数
1
解决办法
118
查看次数