我有一個JFrame
,它包含一個JPanel
,其中包含JScrollPane
,其中包含另一個JPanel
,與兩個組件(JPanels
)。 出於某種原因,當我使用WindowBuilder的預覽選項來查看框架時,JScrollPane
顯示了滾動條水平,但是當我編譯並運行應用程序時,它不會。 這裏是什麼樣子:JScrollPane不工作/顯示滾動條
從預覽選項:
當它編譯:
這裏是我的代碼:
package home;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.SystemColor;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Font;
import javax.swing.BoxLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.ScrollPaneConstants;
public class AtmManeger implements Serializable {
private JFrame frmAtmManeger;
public int NumOfOpenAtmMachines = 0;
private final AtmManeger frame = this;
private ArrayList<ATMmachine> ATMs = new ArrayList<ATMmachine>();//Array list of all the ATM machines that were opened
private AtmAccountDataBase atmDataBase = new AtmAccountDataBase();
private JLabel contLabel = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AtmManeger window = new AtmManeger();
window.frmAtmManeger.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AtmManeger getFrame() {
return this.frame;
}
public AtmManeger() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frmAtmManeger = new JFrame();
frmAtmManeger.setMinimumSize(new Dimension(615, 420));
frmAtmManeger.getContentPane().setBackground(SystemColor.activeCaption);
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(10, 10));
frmAtmManeger.setContentPane(mainPanel);
JScrollPane scrollPane = new JScrollPane();
mainPanel.add(scrollPane, BorderLayout.CENTER);
JPanel subPanel = new JPanel();
subPanel.setSize(new Dimension(1190, 350));
subPanel.setBackground(SystemColor.activeCaption);
subPanel.setPreferredSize(new Dimension(1190, 350));
scrollPane.setViewportView(subPanel);
subPanel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
JPanel panel = new JPanel();
subPanel.add(panel,"cell 0 0,grow");
panel.setBorder(null);
panel.setBackground(SystemColor.textHighlight);
panel.setLayout(new MigLayout("", "[100.00%,grow]", "[71px][13.54%][10.09%][41px][][grow]"));
JPanel panel_1 = new JPanel();
subPanel.add(panel_1, "cell 1 0,grow");
frmAtmManeger.setTitle("ATM Maneger");
frmAtmManeger.setBounds(700, 400, 609, 420);
frmAtmManeger.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我製作了另一個與這個應用非常相似的應用,只有一個Inherents JFrame
。我無法找到此代碼和上面的代碼之間的任何區別。代碼:
package home;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.FlowLayout;
import net.miginfocom.swing.MigLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JButton;
public class test02 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test02 frame = new test02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public test02() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Handle exception
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 432, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 220));
panel.setBackground(Color.RED);
scrollPane.setViewportView(panel);
panel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
JPanel panel_1 = new JPanel();
panel.add(panel_1, "cell 0 0,grow");
panel_1.setLayout(new MigLayout("", "[][][][][][][]", "[][][][][][]"));
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel, "cell 2 1");
JButton btnNewButton_1 = new JButton("New button");
panel_1.add(btnNewButton_1, "cell 3 3");
JButton btnNewButton = new JButton("New button");
panel_1.add(btnNewButton, "cell 2 5");
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1, "cell 6 5");
JPanel panel_2 = new JPanel();
panel.add(panel_2, "cell 1 0,grow");
}
}
所以這可能只是一個愚蠢的錯誤,但它是什麼?
我不使用'MigLayout',但如果你願意放棄它,我會發佈一個答案。 – user1803551 2014-10-19 08:52:43