jframe

Background image in a JFrame

空扰寡人 提交于 2019-11-27 15:49:23
This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paintComponent, like so: class BackgroundPanel extends JPanel { private ImageIcon imageIcon; public BackgroundPanel() { this.imageIcon = Icons.getIcon("foo"); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(imageIcon.getImage(), 0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight(),this); } } But now, how do you add a component on top of that background? When I go JFrame w = new

How to set a JFrame size to fit the CardLayout displayed JPanel?

蹲街弑〆低调 提交于 2019-11-27 15:15:59
问题 I have a JFrame containing a set of JPanels in a CardLayout . Each JPanel has a different size and I want the JFrame to adapt to the size of the currently displayed JPanel (not the JPanel to adapt to the size of the JFrame ). How can I achieve this? 回答1: The general is: if you have a layout problem, always solve it with an appropriate LayoutManager. Never tweak a component's sizing hint to reach your goal. In this case, it's particularly easy to adjust the CardLayout. By default, it

How to add JCheckBox in JTable?

风格不统一 提交于 2019-11-27 15:14:39
First of all i apologize for my english neglect that i will explain all my problem. First i want JCheckBox in the JTable i have. I am retrieving student id and student name from database in column index 0 and 1. I want third column should be Absent/Present which will initially take whether student is present or absent by JCheckbox Value. Here my code for JTable values : Attendance.java /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package shreesai; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import

How to focus a JFrame?

感情迁移 提交于 2019-11-27 15:07:56
I am writing a small game, with one JFrame that holds the main game, and another JFrame that displays the score. the problem is, when I am done constructing them, the score JFrame always ends up focused! I have tried calling scoreDisplay.toFront(), scoreDisplay.requestFocus(), and even: display.setState(JFrame.ICONIZED); display.setState(JFrame.NORMAL); Is there any way to make this work? Thanks in advance, john murano Have you consider setting the score in the same frame as the game frame? Other possible ( quick and dirty ) option is to create them in reverse order, or at least ( if score

How to fit Image size to JFrame Size?

别说谁变了你拦得住时间么 提交于 2019-11-27 14:48:49
I have a JPanel into a JFrame . I loaded a picture on the JPanel but its shown just a part of the picture: This is the part of the code where i did it: JPanel panelImg = new JPanel() { public void paintComponent(Graphics g) { Image img = new ImageIcon("Welcome.png").getImage(); Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); setSize(size); setLayout(null); g.drawImage(img, 0, 0, null); } }; mainFrame.add(panelImg); So this is how it looks like: The complete picture looks like this: Is there a way to

JFrame: get size without borders?

一世执手 提交于 2019-11-27 14:35:53
In Java, is it possible to get the Width and Height of the JFrame without the title and other borders? frame.getWidth() and frame.getHeight()1 seems to return the width including the border. Thanks. frame.getContentPane().getSize(); frame.pack(); System.out.println("frame width : "+getWidth()); System.out.println("frame height: "+getHeight()); System.out.println("content pane width : "+getContentPane().getWidth()); System.out.println("content pane height: "+getContentPane().getHeight()); System.out.println("width of left + right borders: "+(getWidth()-getContentPane ().getWidth())); System.out

Java Swing adding Action Listener for EXIT_ON_CLOSE

非 Y 不嫁゛ 提交于 2019-11-27 14:18:44
I have a simple GUI: public class MyGUI extends JFrame{ public MyGUI(){ run(); } void run(){ setSize(100, 100); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// maybe an action listener here } } I would like to print out this message: System.out.println("Closed"); When the GUI is closed (when the X is pressed). How can I do that? Try this. addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("Closed"); e.getWindow().dispose(); } }); ersks Write this code within constructor of your JFrame : this

How to maximize a JFrame through code?

好久不见. 提交于 2019-11-27 14:18:22
问题 How to maximize a JFrame through code? 回答1: Try this: f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH ); 回答2: this works perfectly till java 7 public class TEST { public static void main(String args[]) { JFrame jf= new JFrame(); jf.setVisible(true); jf.setExtendedState(JFrame.MAXIMIZED_BOTH); } } 回答3: Yea the Toolkit solution ignores the windows task bar and uses the full screen which is not what you want. For an immediate maximise of your form just add this in the JFrame

How to minimize a JFrame window from Java?

给你一囗甜甜゛ 提交于 2019-11-27 14:18:05
In my Java app, I have a JFrame window, how can I minimize it from my Java program ? minimize with frame.setState(Frame.ICONIFIED) restore with frame.setState(Frame.NORMAL) Minimize: frame.setState(Frame.ICONIFIED); Another way to minimize: frame.setExtendedState(JFrame.ICONIFIED); Normal size: frame.setState(Frame.NORMAL); Another way to normal size: frame.setExtendedState(JFrame.NORMAL); Maximize: frame.setState(Frame.MAXIMIZED_BOTH); Another way to maximize: frame.setExtendedState(JFrame.MAXIMIZED_BOTH); Full Screen maximize: GraphicsDevice device = GraphicsEnvironment

Drag and Resize undecorated JFrame

和自甴很熟 提交于 2019-11-27 13:46:29
Currently, I am using the following code to drag and move my undecordated JFrames. private void initialiseGUI(Component component){ //<editor-fold defaultstate="collapsed" desc="code"> component.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { posX = e.getX(); posY = e.getY(); } }); component.addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent evt) { //sets frame position when mouse dragged Rectangle rectangle = getBounds(); getGUI().setBounds(evt.getXOnScreen() - posX, evt.getYOnScreen() - posY, rectangle.width, rectangle.height); } })