jframe

How to change JFrame icon [duplicate]

有些话、适合烂在心里 提交于 2019-11-26 04:41:38
问题 This question already has an answer here: How to set Icon to JFrame 11 answers I have a JFrame that displays a Java icon on the title bar (left corner). I want to change that icon to my custom icon. How should I do it? 回答1: Create a new ImageIcon object like this: ImageIcon img = new ImageIcon(pathToFileOnDisk); Then set it to your JFrame with setIconImage(): myFrame.setIconImage(img.getImage()); Also checkout setIconImages() which takes a List instead. 回答2: Here is an Alternative that worked

KeyListener, keyPressed versus keyTyped

杀马特。学长 韩版系。学妹 提交于 2019-11-26 04:25:28
问题 I have a JFrame (well, a class which extends JFrame) and I want to do an action when I press the F5 key. So, I made the class implement KeyListener. And with that, came three methods, keyPressed, keyReleased, and keyTyped. Which of these methods should I use to listen for F5 being pressed? keyPressed or keyTyped? I currently have the following, however it does not print anything when I press F5. public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_F5) System.out.println(\"F5

Get a Swing component by name

我怕爱的太早我们不能终老 提交于 2019-11-26 03:58:21
问题 I have in a JFrame some components that I want to refer into another JFrame and I want to get them by name and not do public get/set methods for each. Is there a way from Swing to get a component reference by its name like do c#? e.g. form.Controls[\"text\"] Thanks 回答1: I know this is an old question, but I found myself asking it just now. I wanted an easy way to get components by name so I didn't have to write some convoluted code each time to access different components. For example, having

How do I save preference user settings in Java?

此生再无相见时 提交于 2019-11-26 03:56:56
问题 For example, I have a window with a preference button. I want to make it so that when user press the preference button and checks his/her appropriate options and press ok, it saves the preference, then when user presses run on the main window, it runs accordingly to preference the user changed on the preference window. Thank you in advance. 回答1: You can use java.util.prefs package. A simple example: // Retrieve the user preference node for the package com.mycompany Preferences prefs =

Limiting the number of characters in a JTextField

白昼怎懂夜的黑 提交于 2019-11-26 03:39:35
问题 I want to set the maximum length of a JTextField , so that you can\'t enter more characters than the limit. This is the code I have so far... textField = new JTextField(); textField.setBounds(40, 39, 105, 20); contentPane.add(textField); textField.setColumns(10); Is there any simple way to put a limit on the number of characters? 回答1: You can do something like this (taken from here): import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing

Show JFrame in a specific screen in dual monitor configuration

假装没事ソ 提交于 2019-11-26 02:54:51
问题 I have a dual monitor config and I want to run my GUI in a specific monitor if it is found. I tried to create my JFrame window passing a GraphicConfiguration object of my screen device, but it doesn\'t work, frame still display on the main screen. How can I set the screen where my frame must be displayed? 回答1: public static void showOnScreen( int screen, JFrame frame ) { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices();

JTable not showing up on JFrame (Java)

蹲街弑〆低调 提交于 2019-11-26 02:39:21
问题 I\'m having a problem with a JFrame not showing a JTable that is added to it. I\'ve tried getContentPane().add(..) , I\'ve switched to just add to keep the code a little shorter. Any help is more than appreciated! package com.embah.Accgui; import java.awt.*; import javax.swing.*; public class accCreator extends JFrame { private String[] columnNames = {\"Username\", \"Password\", \"Members\", \"World\"}; private Object[][] data = {{\"b\", \"b\", \"b\", \"b\"}, { \"e\", \"e\", \"e\", \"e\"}};

Why does the first panel added to a frame disappear?

自闭症网瘾萝莉.ら 提交于 2019-11-26 02:21:34
Below is an example of adding two panels to a frame. Only one panel (the 2nd, red panel) appears. Why does the first panel disappear? import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyBorder; public class DisappearingPanelInFrame { DisappearingPanelInFrame() { JFrame f = new JFrame(this.getClass().getSimpleName()); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.add(new ColoredPanel(Color.GREEN)); f.add(new ColoredPanel(Color.RED)); f.pack(); f.setVisible(true); } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() {

JPanel repaint issue

心已入冬 提交于 2019-11-26 02:13:59
I have a JFrame which contains 2 JPanel subclass and 2 JLabel in BorderLayout. One of the JPanel contains JButtons and the other is used for displaying graphics. The JLabels are in north and south, the button JPanel in the west and the display JPanel in center. The display JPanel requires constant refresh, so i invoke its repaint() method via the action event generated by swing timer. I also override its paintComponent() method to do my drawings. Instead of displaying what i have drawn, the "content of the JFrame" is being drawn onto the display JPanel. I am aware that i can simply "clear" the

Sizes of frame icons used in Swing

天涯浪子 提交于 2019-11-26 02:11:02
问题 We can use a list to initialise the window icons using Window.setIconImages(List<? extends Image>). What are the different sizes of icons typically used for in a JFrame ? Code This code turns 64 different sized images (from 16x16, incrementing by 2) into icons for the list. import java.awt.*; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Vector; import javax.swing.*; import javax.swing.border.EmptyBorder; public class FrameIconList { public static