awt

How does java.awt.Color.getColor(String colorName) work?

强颜欢笑 提交于 2019-12-28 06:44:13
问题 I'm trying to get colors by name, and I came across Converting a String to Color in Java, which suggests using java.awt.getColor . I can't work out what to pass it as a string though. The following System.out.println( java.awt.Color.getColor( "black", Color.red ) ); prints out java.awt.Color[r=255,g=0,b=0] i.e. it is going with the default color in there. I've put this in a text box, and tried alternative capitalisations etc. The docs aren't very helpful here. Can anyone suggest what magic

How do I make a rectangle move across the screen with key bindings?

北慕城南 提交于 2019-12-28 03:12:18
问题 The game I'm trying to create is snake and so far I've figured out how to use paint(Graphics g) a bit of JPanel , mouse listener and now I'm trying to create a rectangle that will move across the screen and use key bindings or key listener, but I have no idea how I should go about this. Here's my code so far, it has 2 parts. The first part is called snake2 because if I don't know what I'm doing I make the same program with different things. Snake used frame, but Snake2 uses JPanel (looks

Resize image with JFrame window resize in java

戏子无情 提交于 2019-12-25 18:57:15
问题 I have 10 images in one JPanel in JFrame. I want to resize all these images when user resize the JFrame window. 回答1: Use a GridLayout to display the images. The grid will resize as the frame is resized. Use Darryl's Stretch Icon to display the Icon on a JLabel. The Icon will automatically resize itself. 来源: https://stackoverflow.com/questions/28541974/resize-image-with-jframe-window-resize-in-java

Error when adding event handler into a JButton to repaint the image inJava GUI

风流意气都作罢 提交于 2019-12-25 17:03:13
问题 I've created 2 JButtons.One of them has the function of a button and the other handles an image.I want that image to change when this button is clicked.So inserted the method repaint() and added a listener to the first JButton to change the image.When trying to add the listener or the event handler to the first JButton nothing happens.So the image doesn't change.Can anyone show me how can I insert this listener in a way that it works(changes the image when the button is clicked)?Here is my

icon in JButton is not shown at the running of the program, what could be?

隐身守侯 提交于 2019-12-25 15:28:22
问题 I'm training with Java, especially with GUI (Swing & AWT), but I'm having a problem with icon in JButton. The image isn't shown, to make that visible I must resize the window, how could I do? Here is the code: public class MainWindow extends JFrame implements WindowListener, KeyListener, ActionListener, FocusListener, MouseListener{ private final String APPLICATION_NAME = "GUI"; private final String APPLICATION_VERSION = "0.0.1"; private final JButton btnCiao; JPanel panel = new JPanel();

How to use keyListener properly in Java

这一生的挚爱 提交于 2019-12-25 07:58:37
问题 I am running into problems trying to use KeyListener in java. I am trying to write a program that runs until a key is pressed, and then outputs that key. This is a stepping stone to more elaborate code, so the use of a method to print the key pressed is just being used as a kind of prototype. Here is the code: import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class keylistener implements KeyListener{ public keylistener(){ addKeyListener(this); } public void keyPressed

Java graphics isn't repainting

半世苍凉 提交于 2019-12-25 07:27:04
问题 I want to add in a feature so that when the mouse goes over a button then it will add a drop shadow. Currently I'm just trying to get the mechanics to work. I have my game loop calling a update method which I know works but here it is anyway public void updateManager(double delta){ mhandler.updateCoordinates(); if(mhandler.getX() >= 144 && mhandler.getX() <= 444 && mhandler.getY() >= 784 && mhandler.getY() <= 980){ oversp = true; }else{ oversp = false; } } mhandler is what I named my

Trying to use key Listener

白昼怎懂夜的黑 提交于 2019-12-25 06:43:53
问题 I have a program that implements keyListener. What I'm trying to do is only allow digits, backspace key, decimal key, and back arrow key to be pressed. Everything works except when I pressed the back arrow key it deletes the numbers I input` public void keyReleased(KeyEvent e) { try{ if(!Character.isDigit(e.getKeyChar()) && e.getKeyChar() != '.' && e.getKeyChar() != e.VK_BACK_SPACE && e.getKeyChar() != KeyEvent.VK_LEFT){ String input = inputIncome.getText(); inputIncome.setText(input

Applet - KeyListener not working

南楼画角 提交于 2019-12-25 06:30:57
问题 When I start up my applet, I get no response from the key listener. How do I fix this? Below is the code. import java.applet.Applet; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Timer; import java.util.TimerTask; public class Main extends Applet { private static final long serialVersionUID = 1L; public String mode = ""; public Graphic gr; public void init() { this.setSize(400, 400); gr = new Graphic(); this.add(gr); gr

onclick i want to get the name of the JPanel that is present on the JFrame

别说谁变了你拦得住时间么 提交于 2019-12-25 05:54:54
问题 Below is what im trying to implement but its giving error on the line mentioned ; this is because the function doesnot get which Component.getName() is of ... String name=new String(); mntmOneToOne.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { String name=new String(); int count = arg0.getClickCount(); if (count == 1) { Component panel = (Component) arg0.getSource(); System.out.println(panel.getName()); } } }); 回答1: Get the clicked object via your