layout-manager

Looking for general method for GridBagLayout component creation

假装没事ソ 提交于 2019-11-27 02:17:18
I'm designing a GUI with 20 or so components: 10 labels, 4 text fields, 4 buttons, and 2 text areas. Using GridBagLayout seemed a great idea. But with all the instance variables required to do it by the book for each component (i.e., not reuse), a general method for adding components seemed a must. I really thought this could work: (Note: HORIZ is abbreviation for GridBagConstraints.HORIZONTAL; CENTER is abbreviation for GridBagConstraints.CENTER.) public static void addComponent(Container f, Component c, int x, int y, int w, int h, int ipadx, int ipady, float wtx, float wty, int fill, int

FlowLayout on top of GridLayout not working

纵饮孤独 提交于 2019-11-27 02:14:49
I'm trying to create a hangman game and so far it's coming along GREAT, but the layout design just doesn't seem to fall into place! The alphabet is supposed to end up in a FlowLayout order on top of the Hangman picture with the buttons "Restart", "Help" "Add New Word" and "Exit" at the bottom! What am I doing wrong? import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class Hangman extends JFrame { int i = 0; static JPanel panel; static JPanel panel2; static JPanel panel3; public Hangman() { JButton[] buttons = new JButton[26]; panel = new JPanel(new

What's so special about CardLayout vs manual adding/removal of JPanels?

空扰寡人 提交于 2019-11-27 02:13:00
问题 There have been many times on StackOverflow where a user asks a question like this... I have a main JPanel that contains a child JPanel . When the user clicks a button, the child JPanel should change to a different JPanel . How can I achieve this. More often than not, the user has actually tried to implement this problem, but can't get it working. Whenever I answer this question, I tell them to do something like this (put simply)... JPanel myFrame = new JPanel(); myFrame.remove(oldPanel);

adding multiple jPanels to jFrame

时光毁灭记忆、已成空白 提交于 2019-11-27 01:57:04
问题 I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs. public guiCreator() { setTitle("Passenger Seats"); //setSize

Java - Vertical “FlowLayout” with Horizontal Scrolling

那年仲夏 提交于 2019-11-27 01:36:29
问题 As described in the title, I've been trying to set up sort of a vertical flow layout with horizontal scrolling. The components within the layout will be JLabels. Let me draw a picture: +-------------------------+ <--- window |Label1 Label4 Label7| |Label2 Label5 Label8| <--- labels |Label3 Label6 Label9| |<===============>========| <--- scrollbar +-------------------------+ Same window, expanded vertically +--------------------------+ <--- window |Label1 Label5 Label9 | |Label2 Label6 Label10

Jframe not the right size when its executed, any reason why?

一个人想着一个人 提交于 2019-11-26 23:43:32
问题 I am making a game but whenever i run the second jFrame I have to resize it in order to get the right size for the second jFrame, anyone know why? here is the method in the first jFrame class that opens the second one: private void playButtonMouseClicked(java.awt.event.MouseEvent evt) { if (playerOneNameText.getText().equals("")) { } if (playerTwoNameText.getText().equals("")) { } else{ pOneName = playerOneNameText.getText(); pTwoName = playerTwoNameText.getText(); ChessBoardUI class1 = new

Make an added JPanel visible inside a parent JPanel

梦想与她 提交于 2019-11-26 23:31:30
问题 How to make an added JPanel visible inside a parent JPanel ? I am using Netbeans for designing my UI. I have a MainFrame.java , which contains two panels; namely headerPanel and bodyPanel . In headerPanel I have put three buttons,let it be button1 , button2 and button3 . Also I have created three separate files extending JPanel , name it panel1 , panel2 and panel3 . Then I added all my three panels inside bodypanel in MainFrame.java in constructor. bodyPanel.add(panel1); bodyPanel.add(panel2)

Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components

六月ゝ 毕业季﹏ 提交于 2019-11-26 23:26:54
问题 I read some posts here and I started why some people do @Override public Dimension getPreferredSize() { return new Dimension(500, 500); } instead of setPreferredSize(new Dimension(500, 500)); Isn't the second one better because it creates only one Dimension object whereas the first one possibly creates several (even if it's not that much wasted memory)? Or am I wrong? Is there a difference at all? 回答1: A big difference is how the value can change over time, and so the one you choose should be

Java CardLayout Main Menu Problem

痴心易碎 提交于 2019-11-26 22:11:06
问题 Ok so im working on this game in java called 8 bit chimera. Im working on the main menu right now but when im using the card layout the window wont open for some reason. Here is some code. import javax.swing.*; import java.awt.*; public class MainScreen extends JFrame{ String Title = "MainMenu"; MainMenuComp MMC = new MainMenuComp(); BreedingGround BGR = new BreedingGround(); public MainScreen() { setTitle("8-bit Chimera "+Title); setSize(800,600); setResizable(false);

jScrollPane can't add component

拜拜、爱过 提交于 2019-11-26 21:54:18
问题 I have a jScrollPane and a button on a form. The button adds a component to the jScrollPane . I'm using a FlowLayout with a center alignment to arrange the components within the jScrollPane . The first component has no problems appearing and is aligned perfectly. When I then hit the button again, nothing seems too happen. When I follow the debugger it shows that everything happens precisely as before. The code that's being executed when the button is clicked: jScrollPane.getViewport().add(new