I'm working on GUI and I'm an amateur programmer. I have a problem with this code. I can't see anything on the frame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1366, 768);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Container midPanel = new JPanel();
midPanel.setLayout(null);
Dimension preferredSize = new Dimension(700, 700);
midPanel.setPreferredSize(preferredSize);
.....
Container k1 = new JPanel();
k1.setSize(50, 700);
k1.setLocation(0, 0);
k1.setLayout(new GridLayout(rowNum, 1));
k1.setVisible(true);
midPanel.add(k1);
.......
Dimension jspD = new Dimension(500,500);
JScrollPane jsp = new JScrollPane(midPanel);
jsp.setPreferredSize(jspD);
jsp.setLocation(0, 0);
jsp.setVisible(true);
contentPane.add(jsp);
I would appreciate your help.
midPanel.setLayout(null);
You should always use Layout Managers, never ever remove the layout for any reason, except if you had an assignment required to use absolute layout (null layout).
The problem is with absolute layout , you have to specify the location of components inside the panel by component.setBounds(x,y,width,height) every time adding a component, otherwise, it won't be visible.
See this tutorial on Using Layout Managers.
Related
so basically, when I add two panes to container with BorderLayout I have a something like padding and I have no idea how to fix it
below the code is a pic of what I mean
Container mainContainer = this.getContentPane(); //
mainContainer.setLayout(new BorderLayout(8, 6));
mainContainer.setBackground(Color.BLACK);
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.CYAN));
JPanel panelZTekstem = new JPanel();
panelZTekstem.setBackground(Color.ORANGE);
poleTekstowe.setEditable(false);
poleTekstowe.setText("0");
poleTekstowe.setSize(400, 100);
poleTekstowe.setOpaque(true);
poleTekstowe.setFont(new Font("MV Boli", Font.BOLD, 20));
poleTekstowe.setHorizontalAlignment(JTextField.RIGHT);
panelZTekstem.setLayout(new FlowLayout());
panelZTekstem.add(poleTekstowe);
mainContainer.add(panelZTekstem,BorderLayout.NORTH);
JPanel panelZLiczbami = new JPanel();
for (int i = 0; i <= 16; i++) {
JButton test = new JButton();
panelZLiczbami.add(test);
}
panelZLiczbami.setBackground(Color.BLUE);
mainContainer.add(panelZLiczbami, BorderLayout.CENTER);
when I add two panes to container with BorderLayout I have a something like padding
mainContainer.setLayout(new BorderLayout(8, 6));
What did you think the 8/6 values are used for?
You are creating a gap between the components.
It is best to read the API to understand how the parameters are used.
I have been using JPanels for a while and now want to place a JPanel on top of another JPanel.
I have looked at using JLayer but I was wondering If there is a solution to just set the layer of the bottom and top, I don't want to set each components layer if possible.
Example
JPanel bottomPanel = new JPanel(); # Set as bottom panel
JPanel topPanel = new JPanel(); # Set as top panel
JPanel sidePanel = new JPanel(); # Don't have to set
JPanel anotherSidePanel = new JPanel(); # Don't have to set
If this isn't possible what is the best solution for this, Thanks.
You can have the main panel use a BorderLayout.
Then you can do something like:
mainPanel.add(leftSide, BorderLayout.LINE_START);
mainPanel.add(rightSide, BorderLayout.LINE_END);
JLayeredPane lp = new JLayeredPane();
mainPanel.add(lp, BorderLayout.CENTER);
It sounds like what you want is a layout manager. There are a few different ones that suit different needs. There's a link at the bottom of this post.
My personal favorite is GridLayout. So for what you want to do, you can do this:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
//the first number is the number of rows, the second is the number of columns
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
panel.add(topPanel);
panel.add(bottomPanel);
That will do what you want.
If you wanted to read more about them, here's a link:
Oracle Docs on Layout Managers
I know this is quite late, but if anyone now has this issue, I suggest using a BoxLayout. BorderLayout can only have one cell in each of its five locations, and GridLayout's cells are all the same dimension. If you want to stack different sized JPanels, here's how BoxLayout can be implemented:
JFrame frame = new JFrame("Intro to BoxLayout");
JPanel container = new JPanel();
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(X1, Y1));
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(X2, Y2));
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(panel1);
container.add(panel2);
frame.add(container);
frame.pack();
frame.setVisible(true);
where X1, Y1, X2, Y2 are arbitrary panel dimensions.
I'm trying to make a text-based adventure game where the top of the screen is a JTextArea inside a JScrollPane that shows what is happening, and the bottom is a JOptionPane where you click on a button to make a choice. By default, the buttons are arranged horizontally. The only problem is that if I have too many buttons, there is no room for new ones and they are pushed off the screen. I need them to be arranged vertically since they are fatter than they are tall. The JOptionPane and the JScrollPane are nested in a gridLayout, which is nested in a JFrame. This is the method I am using to make the frame:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
optionPane = new JOptionPane("", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, null);
contentPane.add(optionPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
Instead of using a JOptionPane, use JButtons in a GridLayout. You can specify how many components you want across and down upon creation like this: new GridLayout(0, 3). This would result in 3 buttons stacked on top of each other, the first int being how many you want across, and the second, how many you want down. Try this:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
//This replaces your JOptionPane block
buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(0, 1));
contentPane.add(buttonPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
I searched a lot on google on this subject, but just can't come out with right solution. I tried "painting" with Graphics paintComponent and everything seems fine, but lines just doesn't appear on my JPanel.
Part of my code with JLabels created:
frame = new JFrame();
frame.setTitle("New family tree");
...
JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
frame.getContentPane().add(panel, BorderLayout.EAST);
panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));
JButton newPersonButton = new JButton("New Person");
panel.add(newPersonButton, "cell 0 5");
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
scrollPane = new JScrollPane();
tabbedPane.addTab("Tree", null, scrollPane, null);
panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new MigLayout("",
"[][][][][][][][][][][][][][][][][]",
"[][][][][][][][][][][][][][][][][][][][][]"));
final JLabel lblAddGreatgrandmother = new JLabel("Add Great-grandmother");
panel_1.add(lblAddGreatgrandmother, "cell 3 4,growx");
final JLabel lblAddGrandmother_1 = new JLabel("Add Grandmother");
panel_1.add(lblAddGrandmother_1, "cell 2 5");
Should I use painting? Or put JLabels in array list and use Point? I'll appriciate any help.
EDIT: Runnable example - http://pastebin.com/NFug1QA1
The problem with the java-sl.com/connector solution is that the connection itself becomes a component- ie needs layout, accepts events etc. I put together a solution for this years ago, which you can find in my sourceforge project. Specifically, see ConnectionPanel and Connection.
... Also, just in my experience, they kinds of connections are most useable when the layout manager is set to null and components are not restricted to being locked into position. But you will know what's best for your case.
I've bee teaching myself java and following along with the problems in the book. I'm trying to make a display for my calculator. In the example(I did not attach this) the buttons were a smaller size than what mine are and I can't figure out how to reformat them. I tried using the dimension class but it had no affect. Also, I can't get my text at the top of the calculator to align left.
Here is my code:
public class Calculator extends JFrame {
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 300);
setLayout(new BorderLayout());
JPanel numberPanel = new JPanel();
add(numberPanel, BorderLayout.CENTER);
numberPanel.setLayout(new GridLayout(4, 3, 3, 3));
for(int i = 1; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
numberPanel.add(button);
}
JButton zero = new JButton("" + 0);
JButton dot = new JButton(".");
JButton clear = new JButton("C");
numberPanel.add(zero);
numberPanel.add(dot);
numberPanel.add(clear);
JPanel keyPanel = new JPanel();
add(keyPanel, BorderLayout.EAST);
keyPanel.setLayout(new GridLayout(4, 1, 3, 3));
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton times = new JButton("*");
JButton divide = new JButton("/");
keyPanel.add(plus);
keyPanel.add(minus);
keyPanel.add(times);
keyPanel.add(divide);
JPanel equalsPanel = new JPanel();
add(equalsPanel, BorderLayout.SOUTH);
equalsPanel.setLayout(new GridLayout(1, 1));
JButton equals = new JButton("=");
equalsPanel.add(equals);
JPanel textPanel = new JPanel();
add(textPanel, BorderLayout.NORTH);
JTextField inputBox = new JTextField("0.0");
inputBox.setHorizontalAlignment(JTextField.LEFT);
inputBox.setEditable(false);
Font font = new Font("MonoSpaced", Font.BOLD, 20);
inputBox.setFont(font);
textPanel.add(inputBox);
setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
}
Imports were left off for brevity
GridLayout will laugh at you when you try and set a dimension. It does respect preferred sizes. You should select a layout manager that will respect preferred sizes. Or you can simply pack() (after you add all your components) your frame instead of setSize() and all the components preferred sizes will kick in. (Disclaimer - because of GridLayout though, if you try and resize the frame after that, you components will resize again)
See more at How to use Layout Managers. For a quick view of which layout managers respect preferred sizes and which ones don't, have a look at this post.
A common approach is to nest panels with different layout managers also, as seen here
UPDATE
As mentioned preciously, you should just call pack on the frame instead of set size. With your current code, this would cause the frame to be very small because of the preferred sizes of the components. If you want the buttons to have a bigger preferred size, you can set the font to a bigger font and/or use button.setMargins(new Insets(w,x,y,x)); to make the margins bigger. But it is preferred to pack the frame.
I would recommend using the Window Builder add-on if you’re using Eclipse. This tool will help you with many aspects of Swing. Learn by doing.
WindowBuilder Dowload Link