我有JFrame
,其中包含JPanel
,其中包含JScrollPane
,其中包含另一个JPanel
,其中包含两个组件(JPanels
)。
出于某种原因,当我使用WindowBuilder的预览选项查看框架时,JScrollPane
会显示水平滚动条,但是当我编译并运行应用程序时,它并没有&# 39;吨。
这是它的样子:
来自预览选项:
编译时:
这是我的代码:
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);
}
}
我制作的另一个应用程序与此非常相似,只有一个应用程序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");
}
}
所以这可能只是一个愚蠢的错误,但它是什么?
答案 0 :(得分:0)
我没有看到您更改滚动条策略。 由于没有溢出,如果不更改该设置,您将看不到滚动条。
尝试:
scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
答案 1 :(得分:0)
我已根据要求编写了一个显示面板的程序。
<强>代码:强>
private void createUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JScrollPane pane = new JScrollPane();
JPanel leftPnl = new JPanel(new BorderLayout());
JPanel rightPnl = new JPanel(new BorderLayout());
leftPnl.add(new JLabel("Left"), BorderLayout.CENTER);
leftPnl.setBorder(new LineBorder(Color.black, 5));
leftPnl.setPreferredSize(new Dimension(400, 400));
rightPnl.add(new JLabel("Right"), BorderLayout.CENTER);
rightPnl.setBorder(new LineBorder(Color.black, 5));
rightPnl.setPreferredSize(new Dimension(400, 400));
panel.add(leftPnl);
panel.add(rightPnl);
panel.add(pane);
frame.setPreferredSize(new Dimension(400, 400));
frame.setTitle("ScrollPane Example");
frame.add(new JScrollPane(panel));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}