问题
I'm making an application to act as a hub of some sorts, where the user can store shortcuts to their favorite applications and easily launch them. I'm having some problems with FlowLayout
, though. When I use GridLayout
, the components display perfectly. When I use FlowLayout
, nothing displays at all.
GridLayout:
FlowLayout:
All I have changed is the LayoutManager
. When I call getComponentCount
, they both respond with 9.
I thought this post was pretty long, so I put a snippet of my code on Code Tidy (from Pastebin)
Thank you in advance for your help!
回答1:
1) FlowLayout
pretty accepting PreferredSize
that came from JComponent
, each of JComponents
can have got different Dimension
on the screen
example (uncomnent getMinimumSize
& getMinimumSize
)
import java.awt.*;
import javax.swing.*;
public class CustomComponent extends JFrame {
private static final long serialVersionUID = 1L;
public CustomComponent() {
setTitle("Custom Component Test / BorderLayout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public void display() {
add(new CustomComponents0(), BorderLayout.NORTH);
add(new CustomComponents0(), BorderLayout.CENTER);
add(new CustomComponents0(), BorderLayout.SOUTH);
add(new CustomComponents0(), BorderLayout.EAST);
pack();
// enforces the minimum size of both frame and component
setMinimumSize(getMinimumSize());
setPreferredSize(getPreferredSize());
setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
CustomComponent main = new CustomComponent();
main.display();
}
};
javax.swing.SwingUtilities.invokeLater(r);
}
}
class CustomComponents0 extends JLabel {
private static final long serialVersionUID = 1L;
/*@Override
public Dimension getMinimumSize() {
return new Dimension(200, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}*/
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}
2) GridLayou
t create proportional area for every JComponents
, then accepting only JComponent
that have got larger Dimnesion
came from PreferredSize
3) for GridLayout
I'm talking about method pack()
, not if is there JFrame#setSize()
, for FLowLayout
doesn't matter,
回答2:
Hmmm I know this has been answered, but just to add, if you look at the code below and run it, it will create 9 labels and 4 buttons and add them using the flow layout, but as with this example below the frames size is set using setSize(int width,int height)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FlowLayoutTest extends JFrame {
private JPanel NorthPanel, SouthPanel;
private JLabel[] labels;
private JButton[] buttons;
public FlowLayoutTest() {
createAndShowUI();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FlowLayoutTest flowLayoutTest = new FlowLayoutTest();
}
});
}
private void createAndShowUI() {
setTitle("Flow layout");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initializeComponents();
addComponents(this.getContentPane());
//pack();
setVisible(true);
}
private void initializeComponents() {
labels = new JLabel[9];
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel("Label " + (i+1));
}
buttons = new JButton[4];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("Button " + (i+1));
}
NorthPanel = new JPanel(new FlowLayout(5));
SouthPanel = new JPanel(new FlowLayout(5));
}
private void addComponents(Container contentPane) {
for (int i = 0; i < buttons.length; i++) {
SouthPanel.add(buttons[i]);
}
for (int i = 0; i < labels.length; i++) {
NorthPanel.add(labels[i]);
}
contentPane.add(NorthPanel, BorderLayout.NORTH);
contentPane.add(SouthPanel, BorderLayout.SOUTH);
}
}
however when you run the application you will notice a label (number 9) has gone off screen, this is because setSize()
was used, however if we call(or in this case uncomment) pack()
before setting the frame to visible, you will be able too see all the components on the frame.
来源:https://stackoverflow.com/questions/11071421/flowlayout-not-displaying-components-while-gridlayout-does