jbutton

How to create on click event for buttons in swing?

旧时模样 提交于 2020-01-23 07:44:33
问题 My task is to retrieve a value of text field and display it in a alert box when clicking on the button. how to generate the on click event for button in java swing ? 回答1: For that, you need to use ActionListener, for example: JButton b = new JButton("push me"); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //your actions } }); For generating click event programmatically, you can use doClick() method of JButton : b.doClick(); 回答2: First, use

Trouble having 2D Array of buttons click themselves randomly using the computer and not user inputs [duplicate]

跟風遠走 提交于 2020-01-22 03:44:25
问题 This question already has answers here : doClick(), and Simon: All buttons will unpress at the same time instead of individually (1 answer) Java Swing blinking button sequence (2 answers) Java game, restart on user command when game is over (1 answer) Issues with GridLayout (1 answer) Closed 3 days ago . I'm currently making a 2D array of buttons (3x3) and need some help having the program click a random button by itself. I'm currently attempting to use doClick() without any success. I need

BoxLayout refuses to honor preferred size of JButton

混江龙づ霸主 提交于 2020-01-21 15:26:45
问题 I have been working on a small project that is supposed to simulate a gambling game. Unfortunately, I ran into some odd issues while working with BoxLayout . To the best of my knowledge, LayoutManager s usually honor any component's preferred size. However, in the below code, BoxLayout does not. Here is my code thus far: import java.awt.*; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(

BoxLayout refuses to honor preferred size of JButton

混江龙づ霸主 提交于 2020-01-21 15:25:33
问题 I have been working on a small project that is supposed to simulate a gambling game. Unfortunately, I ran into some odd issues while working with BoxLayout . To the best of my knowledge, LayoutManager s usually honor any component's preferred size. However, in the below code, BoxLayout does not. Here is my code thus far: import java.awt.*; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(

Swing/Java: How to use the getText and setText string properly

吃可爱长大的小学妹 提交于 2020-01-21 07:01:30
问题 I'm trying to make input nameField appear in a Label called label1 after a Button called button1 is clicked. Right now it says: 'txt' and I understand why. But I don't know how I can use the string! Can anyone explain me what I'm doing wrong and how to use this string properly? import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel;

Accessing locally defined JButtons in a GridLayout JPanel

馋奶兔 提交于 2020-01-19 04:09:09
问题 Lets say you have a GridLayout of JButtons in an NxN grid, in code such as this: JPanel bPanel = new JPanel(); bPanel.setLayout(new GridLayout(N, N, 10, 10)); for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { JButton b = new JButton("(" + row + ", " + col + ")"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); bPanel.add(b); } } How would one access each button individually in the grid to change the button's name through

Java Swing JButton Time Delays (Flicker)

我们两清 提交于 2020-01-19 01:54:29
问题 I am trying to make my JButton flicker red for this game I am creating. All the solutions on this website suggest using a thread and putting it to sleep or using a timer, however, the pause allays seems to come after the color change Here is my code: Color cb = board[Y1][X1].getBackground(); board[Y1][X1].setBackground(Color.RED); //Pause board[Y1][X1].setBackground(cb); If I put a thread and put it to sleep on line 3 and comment out line 4 the pause will come before the JButton is turned red

Incorrect position of Swing components

空扰寡人 提交于 2020-01-17 14:17:08
问题 I woud like to have button btn at the bottom of a JFrame . I have this in right side. Where is bug in my code? class MainClass extends JFrame { private JSplitPane splitPan=null; private void treePanel(){ DefaultMutableTreeNode nod=new DefaultMutableTreeNode("AAA",true); nod.add(new DefaultMutableTreeNode("abcd")); JTree tree=new JTree(nod); JScrollPane scroll=new JScrollPane(tree); splitPan=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll,new JLabel("aaaaa")); splitPan.setSize(this

JButton Action Performed?

对着背影说爱祢 提交于 2020-01-17 03:54:06
问题 I have created a Jframe/button with 10x10 grid. Each jbutton is apart of the grid. I am trying to how to affect each button pressed through JFrame/button, as I want to eventually make it into a battleships games. frame.setLayout(new GridLayout(width,length)); grid=new JButton[width][length]; for(int y=0; y<length; y++){ for(int x=0; x<width; x++){ grid[x][y]=new JButton("("+x+","+y+")"); frame.add(grid[x][y]); } } For example I am trying a basic piece of code to see if i can change the color

JLabel and swap the text it holds via the JLabel's setText(…) method

点点圈 提交于 2020-01-16 20:34:11
问题 The question field should contain 36 questions, the answer to these question are on the 36 buttons in the grid. i am having a problem getting question field to display 36 question starting with what is 0 + 1 when the user clicks the correct button it then shows question 2 shows in the field which is what is 1+1 and so till question 36 so how do i get one question JLabel and swap the text it holds via the JLabel's setText(...) method here is my code import java.awt.*; import java.awt.event.*;