jframe

How can I switch between jpanels?

ⅰ亾dé卋堺 提交于 2019-11-27 08:07:20
问题 I'm still very new to java programming, so please help me to correct any mistakes I might have overlooked or give tips on how to improve this program. Okay, so a lot of problems have been solved, and now I have a CardLayout, but I still have questions about how I should make my pipes show inside it. When I tried to add in my refresh rate timer and my speed timer, I have problems about how I need to declare and initialize boolean variables. Also, when I compile and run this game, I get files

Specifying the location of Canvas in Swing

江枫思渺然 提交于 2019-11-27 07:54:39
问题 I'm learning Swing GUI design. One thing I haven't quite sorted out is how to add an add a Canvas to a specific location in a container. More specifically: I create a Canvas class that uses Paint method. Object of this class is added to a Panel. What I do not quite understand, is how and where it is added to a Panel. In Tkinter Canvas is a widget that contains only an image, but in Swing there is no similar widget (probably not the best word) added to the Frame that contains only Canvas

java JFrame graphics

和自甴很熟 提交于 2019-11-27 07:20:30
问题 I have the following simple code in a JFrame constructor super(name); setBounds(0,0,1100,750); setLayout(null); setVisible(true); g = this.getGraphics(); int[] x =new int[]{65, 122, 77, 20, }; int[] y =new int[]{226, 258, 341, 310}; g.setColor(Color.RED); g.drawPolygon (x, y, x.length); System.out.println(g); I get the output on console as: sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=255,g=0,b=0]] But no red polygon drawn

How to add an ImageIcon to a JFrame?

心已入冬 提交于 2019-11-27 07:10:27
问题 I'm trying to add an image to one frame but it seems it does not working. The image created by an ImageIcon from the specified file. The image file is in the seam directory the java file exist. import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class image { public static void main(String args[]) { TimeFrame frame = new TimeFrame(); } } class TimeFrame extends JFrame { //Image icon = Toolkit.getDefaultToolkit().getImage("me

drawing simple rectangles on a Jframe in java

折月煮酒 提交于 2019-11-27 06:58:44
问题 I am extending JFrame like this: public GameFrame() { this.setBounds(30, 30, 500, 500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); initializeSquares(); } private void initializeSquares(){ for(int i = 0; i < 5; i++){ this.getContentPane().add(new Square(i*10, i*10, 100, 100)); } this.setVisible(true); } However, only one square is being drawn on the screen, does anybody know why? also My Square class looks like this: private int x; private int y; private int width; private int height;

What is the relation between ContentPane and JPanel?

此生再无相见时 提交于 2019-11-27 06:44:59
I found one example in which buttons are added to panels (instances of JPanel ) then panels are added to the the containers (instances generated by getContentPane() ) and then containers are, by the construction, included into the JFrame (the windows). I tried two things: I got rid of the containers. In more details, I added buttons to a panel (instance of JPanel ) and then I added the panel to the windows (instance of JFrame ). It worked fine. I got rid of the panels. In more details, I added buttons directly to the container and then I added the container to the window (instance of JFrame ).

Which icon sizes to use with a JFrame's setIconImages() method?

旧城冷巷雨未停 提交于 2019-11-27 06:37:55
问题 Does anyone know which icon sizes to use with the setIconImages() (PLURAL) method for a jFrame so that my app icons display well on all platforms and in all contexts (e.g., window icon, taskbar icon, alt-tab icon, etc.)? I've found an example that uses a 16px-by-16px and a 32px-by-32px image, but do I need to go any bigger? To test, I have also tried adding 64px and 128px versions to the List passed to setIconImages(), but these do not seem to be used on my Windows 7 machine. However, I

setDefaultCloseOperation to show a JFrame instead

南楼画角 提交于 2019-11-27 06:12:39
问题 I am making a word processor application in order to practise Java and I would like it so that when the user attempts to close the appliction, a JFrame will come up asking to save changes. I was thinking about setDefaultCloseOperation() but I have had little luck so far. I would also like it to appear whent he user clicks the "X" on the top right of the window aswell if possible. 回答1: You can set the JFrame DefaultCloseOperation to something like DO_NOTHING, and then, set a WindowsListener to

How to set jframe look and feel

天大地大妈咪最大 提交于 2019-11-27 06:07:54
问题 I am kind of confused on where to put this : try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e){ } I did not extend the JFrame class but used JFrame f = new JFrame(); Thanks :D 回答1: Most common place to put this, is right inside your static void main(String[] args) method. Like so: public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun

How to set JFrame to appear centered, regardless of monitor resolution?

此生再无相见时 提交于 2019-11-27 06:04:08
While working with Java, I find it hard to position my main window in the center of the screen when I start the application. Is there any way I can do that? It doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome. I always did it in this way: Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2); where this is the JFrame involved. JRL Use setLocationRelativeTo(null) This method has a special effect when you pass it a