jpanel

How to move from one Panel to another in a single frame without using CardLayout?

与世无争的帅哥 提交于 2019-12-04 06:37:10
问题 I have three Panels and a single Frame in my program. I want to close/hide the current panel and show/activate the next panel. I am designing a game, hence I do not want to use CardLayout . private void select() { if (currentChoice == 0) { f.remove(gpanel1); gpanel = new GamePanel(); f.add(gpanel); } } Here f is the frame object. gpanel1 and gpanel are the initialized panel objects (constructors have already been called from the frame class). How do I hide the old panel and move to the next

Java layout center with two panels

99封情书 提交于 2019-12-04 06:30:37
问题 In Java, when using the BorderLayout, is it possible to have two panels in the CENTER, but both be visible on the form. Here is my code: guiFrame.add(guiFieldsPanel, BorderLayout.CENTER); guiFrame.add(guiButtonsPanel, BorderLayout.CENTER); guiFrame.setVisible(true); In the above code, both panels are set to the center, yet I can only see the guiButtonsPanel as it is 'on top' of the guiFieldsPanel. Can i group both panels together, and then set them to be displayed in the CENTER? 回答1: See the

Use variables from other class from other file to another in java

心不动则不痛 提交于 2019-12-04 06:27:36
问题 There are two players who will input their name in the JTextFields. What I want to do is that the data I entered from the Welcome frame in Enter.java will be transferred to the JLabels in ActualGame.java. import java.awt.*; import java.awt.event.*; import javax.swing.*; import static javax.swing.JFrame.EXIT_ON_CLOSE; public class Enter extends JFrame implements ActionListener { private String one = ""; private String two = ""; private JTextField txtOne = new JTextField(); private JTextField

Fit size of an ImageIcon to a JButton [duplicate]

喜夏-厌秋 提交于 2019-12-04 05:43:27
问题 This question already has answers here : Resizing icon to fit on JButton in Java? (4 answers) Closed 3 years ago . Hi how can I fit the size of a ImageIcon to a JButton? I want to adjust the size of the ImageIcon to the size of the Button JFrame frame2 = new JFrame("Tauler Joc"); JPanel panell = new JPanel(); ImageIcon icon = new ImageIcon("king.jpg"); JButton jb= new JButton(icon); jb.setBounds(200,200,700,700); panell.add(jb); frame2.add(panell); frame2.setDefaultCloseOperation(JFrame.EXIT

Graphics in repaint draws random lines

南笙酒味 提交于 2019-12-04 05:17:35
问题 So I'm creating a free hand drawing JPanel, that reacts to mouse movements and draws lines. I got it mostly working except for a bug where it'll randomly draw a straight line between lines. That random straight line isn't intentional, what's drawn on the buffered image is supposed to be strictly what the user draws. These random drawn lines are not done by the user and it's confusing. Below is my code, can anyone take a look? The image included gives you a visual representation of what it is

Java Fade In and Out two JPanels at the same time

徘徊边缘 提交于 2019-12-04 05:17:27
问题 I have a list of JPanels that I want to display as a "slideshow" where one JPanel fades out and the next JPanel in the list fades in. This is the code I am fiddling with: public float opacity = 0f; private Timer fadeTimer; private boolean out; public void fadeIn() { out = false; beginFade(); } public void fadeOut () { out = true; beginFade(); } private void beginFade() { fadeTimer = new javax.swing.Timer(75,this); fadeTimer.setInitialDelay(0); fadeTimer.start(); } public void actionPerformed

Set the same font for all component Java

依然范特西╮ 提交于 2019-12-04 05:15:05
I want to set a specific font for all component in a JPanel but I prefer that the question is still more general and not limited to the JPanel. How can I set the font to a list of component in a container (JFrame or JPanel)? Here is a simple method that allows you to specify Font to the whole components tree under any Container (or just a simple Component, doesn't matter): public static void changeFont ( Component component, Font font ) { component.setFont ( font ); if ( component instanceof Container ) { for ( Component child : ( ( Container ) component ).getComponents () ) { changeFont (

BorderLayout doesn't show correctly

霸气de小男生 提交于 2019-12-04 05:07:01
问题 I want to have a JFrame, where on the left and the right there is a border that has the color black and a width of withfOfJFrame/10. Now, my try at it looks like this: JFrame f = new JFrame(); f.setSize(800, 600); f.setLayout(new BorderLayout()); JPanel leftBorder = new JPanel(); JPanel rightBorder = new JPanel(); leftBorder.setBackground(Color.black); rightBorder.setBackground(Color.black); leftBorder.setSize(f.getWidth()/10, f.getHeight()); rightBorder.setSize(f.getWidth()/10, f.getHeight()

how to use jpanel with paint (or repaint)

僤鯓⒐⒋嵵緔 提交于 2019-12-04 04:27:41
问题 I'm a newbie to the paint/graphics and wonder how to add a JPanel to my code in such way that the entire graphics will be on a JPanel and not on the JFrame. In other words, I'm trying to create a GUI that will allow me to do this: on the RIGHT side show the nice movement of the lines on a JPanel on the LEFT side, add a JTextArea (on a JPanel) that will show the coordination of the graphics. This is a simplification of a bigger problem but I guess the code here is easier to understand. Thank

Paint background of JPanel

 ̄綄美尐妖づ 提交于 2019-12-04 03:56:27
问题 How can I tell the paint method to draw background on JPanel only and not on the entire JFrame. My JFrame size is bigger than the JPanel. When I try to paint a grid background for the JPanel, the grid seems to be painted all over the JFrame instead of just the JPanel. Here parts of the code: public class Drawing extends JFrame { JPanel drawingPanel; ........... public Drawing (){ drawingPanel = new JPanel(); drawingPanel.setPreferredSize(new Dimension(600,600)); } public void paint(Graphics g