jbutton

Unicode Character uncompatibility?

ぃ、小莉子 提交于 2019-12-31 04:49:07
问题 I have a problem with character encoding using swing on Java. I want to write this character: "\u2699" That is a gear on a simple JButton but when I start my program I get only a JButton with a square and not the gear. This is the line: opt.setText("\u2699"); Where opt is the button. The button result: Can I change swing character encoding or something else? Thanks. 回答1: As mentioned by Andreas, use a Font that supports that character. But unless supplying a suitable font for the app., the

Find index of button in 2D Array Java?

依然范特西╮ 提交于 2019-12-31 04:41:09
问题 I just have a question on the following. I've got a 2D array of buttons that require me to run another method when I click on them. My current method relies on the following input, a String (which I can get from the user easily) and two integer values. These are dictated by the buttons position in the array. I have a button listener attached to these buttons but I am not too sure how I can work out what the button's position actually is. I've made my own button class (because I wanted some

Align JButton icon to the left and keep text centered

跟風遠走 提交于 2019-12-31 04:00:29
问题 I would like to set up a JButton so that it's icon is aligned to the left of it, while the text is centered. I have found how to have one of them left, the other right, or both at the same setting, but I couldn't find what I am looking for. Of course I can always redefine the paint methods, but I am looking for a leaner way to do it. 回答1: You can add a layout manager to your JButton , for example a Border Layout would help: You create a JLabel with an Icon and one with the text "Click me":

How do I make Java wait for a method to finish before continuing?

我的未来我决定 提交于 2019-12-31 03:19:07
问题 So my problem is that I need these methods to run one after another but I cannot work out how to make the methods wait before being run. Any help is appreciated. Thank you. Here is my code: public void startMoving() throws InterruptedException { moveEnemy("right",3); wait(); moveEnemy("down",3); wait(); moveEnemy("right",2); wait(); moveEnemy("up",1); wait(); moveEnemy("right",2); wait(); moveEnemy("up",2); wait(); moveEnemy("right",2); wait(); moveEnemy("down",4); wait(); moveEnemy("left",1)

Find the index of a Java JButton Array

本小妞迷上赌 提交于 2019-12-31 03:08:09
问题 I am writing a Java program in which I have an array of buttons (not a calculator!) and I'm looking for an efficient way to know which JButton was clicked. From what I know of Java so far, the only way to do it is to have them all in the same ActionListener and then loop through looking for a match. Another solution I just thought of would be to extend JButton to include a unique ID number variable in the constructor. It seems that should work when the event object is cast to JButton after an

Responding to Button using KeyBIndings

我只是一个虾纸丫 提交于 2019-12-31 02:32:37
问题 I want to make a program with these goals: 1) Make a JButton 2) Attach the button to a key (The "A" Key) using KeyBindings 3) Execute some code when "A" is clicked Here is the code I have so far: // Imports Public class Test{ JButton button = new JButton(); //... Test(){ button.getInputMap().put(KeyStroke.getKeyStroke("A"), "Pressed"); //... } // Where do I add the code that responds when button is pressed? } Now where do I add the code that I want it to execute when the button is pressed?

Removing space around buttons in GridBagLayout

落爺英雄遲暮 提交于 2019-12-30 18:29:28
问题 I have this vexing source written to demonstrate a layout for a game screen mentioned on another question. It puts buttons (or labels, choosable at start-up) into a GridBagLayout . If you choose to not use buttons when prompted (before the GUI appears) the entire GUI is nice and compact with no gaps. But if you choose to use buttons it will (if your set up is like mine) look something like this.. Note the red horizontal lines. That is the BG color of the panel showing through. Those lines are

JLabel doesn't show up

雨燕双飞 提交于 2019-12-30 13:11:12
问题 I'm working on a program but my JLabel doesn't show up. My JButton works perfectly (it appears) but for some reason the JLabel does not appear. I have checked on internet but I Haven't found anything. package com.hinx.client; import java.awt.Color; import javax.swing.*; public class Main { public static void main(String [] args) { createWindow(); } static void createWindow() { //Create panel JPanel content = new JPanel(); content.setLayout(null); //Build the frame JFrame frame = new JFrame(

Java - Call Method via JButton

只谈情不闲聊 提交于 2019-12-30 03:12:28
问题 How can I call a method by pressing a JButton? For example: when JButton is pressed hillClimb() is called; I know how to display messages etc when pressing a JButton, but want to know if it is possible to do this? Many thanks. 回答1: If you know how to display messages when pressing a button, then you already know how to call a method as opening a new window is a call to a method. With more details, you can implement an ActionListener and then use the addActionListener method on your JButton.

Hello I am creating a TicTacToe game for myself to understand Java better

浪子不回头ぞ 提交于 2019-12-29 08:25:11
问题 however I am not sure where I am supposed to enter the whoWins() method. Do I enter this method in the actionperformed Method of the buttons or do i need to something different. Please help. public class TTT extends JFrame implements ActionListener { private JButton buttons[] = new JButton[9]; private JButton exitButton; public JLabel title; public JPanel titlePanel, panel; private int count = 0; int symbolCount = 0; private boolean win = false; public TTT() { title = new JLabel("Welcome to