Reputation: 1
How to make specific sizes for Swing components in java? The gui is supposed to look like this...
+++++++++++++++++
+ TextField +
+++++++++++++++++
+ +
+ TextPane +
+ +
+++++++++++++++++
+Button +Button +
+++++++++++++++++
I've tried BorderLayout
, GridLayout
, nested JPanels
, etc, But I can't get a good looking gui where the JButtons
aren't like bricks and the JTextField
isn't three times the size of the text.
Upvotes: 0
Views: 356
Reputation: 1530
The best way to Do that is to use the .setBounds(X, Y, Length, Width) You can set exact positioning of item. It is measured from the top left corner for the X and the Y. For the width it grow what ever the time is to the left and dose not move the X. The same goes with the length and the Y values.
JTextField field = new JTextField(),
field.setBounds(500, 100, 100, 30);
add(field);
Upvotes: -1
Reputation: 168825
Part of it depends on how you want space extra assigned. This GUI provides extra width to the text field and text area, while keeping the buttons centered. Extra height is given to the text area.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
public class BasicLayout {
BasicLayout() {
JPanel gui = new JPanel(new BorderLayout(2,2));
gui.add(new JTextField(), BorderLayout.PAGE_START);
gui.add(new JTextArea(3,15));
JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
controls.add(new JButton("Button1"));
controls.add(new JButton("Button2"));
gui.add(controls, BorderLayout.PAGE_END);
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BasicLayout();
}
});
}
}
At first I thought you meant JTextArea
in the middle part, then I looked more closely. A JTextPane
is slightly more tricky to size because it does not accept size hints in the constructor. For that we can tweak the preferred size of the container for it (in this case, a JScrollPane
.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
public class BasicLayout {
BasicLayout() {
JPanel gui = new JPanel(new BorderLayout(2,2));
gui.add(new JTextField(), BorderLayout.PAGE_START);
JTextPane text = new JTextPane();
JScrollPane scroll = new JScrollPane(text);
Dimension d = text.getPreferredSize();
scroll.setPreferredSize(new Dimension(d.width, d.height*3));
gui.add(scroll);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
controls.add(new JButton("Button1"));
controls.add(new JButton("Button2"));
gui.add(controls, BorderLayout.PAGE_END);
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BasicLayout();
}
});
}
}
Upvotes: 2
Reputation: 6980
If you are looking for designing a GUI in Netbeans, it is possible. Once you have created a Java project, select the project and goto:
File > New File
Then the dialog will appear (that you already know).
In categories, select Swing GUI Forms
and then select whatever you want from the options
Here is a screenshot
Upvotes: 0
Reputation: 5474
A LayoutManager is used to simplify specific layout. in your case it seems that you want to do something specific, so don't use a layout manager!
On the oracle website there is an great guide to layouts. One part of this guide is how to not use it : http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Good Luck with your project!
Upvotes: 0