试图让4 JPanel
s两个在顶部,其他2在底部并且彼此独立地操纵输入文本等...初学者用java所以如果任何人可以帮助我那将是很棒的。我之前发过另一个问题,但现在又被卡住了。洛尔
package project2;
import javax.swing.JOptionPane;
import java.awt.FlowLayout; // specifies how components are arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.JPanel; // Displays a Panel
import javax.swing.SwingConstants; // common constants used with Swing
import javax.swing.Icon; // interface used to manipulate images
import javax.swing.ImageIcon; // loads images
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;
public class GUI
{
private JPanel Checks; //Panel to Hold Checks
private JPanel Transactions;
private JPanel History;
private JPanel Graphics;
private JLabel CLabel;
public void displayGUI()
{
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(
new GridLayout(2, 2, 5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
Checks = getPanel(Color.YELLOW.darker().darker());
Transactions = getPanel(Color.RED);
History = getPanel(Color.GREEN.darker().darker());
Graphics = getPanel(Color.MAGENTA);
CLabel = new JLabel("Label with on red text");
contentPane.add(Checks);
contentPane.add(Transactions);
contentPane.add(History);
contentPane.add(Graphics);
frame.setSize(1400,690); //set frame size
frame.setVisible(true); //display frame
frame.setContentPane(contentPane);
}
private JPanel getPanel(Color bColor) {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(bColor);
return panel;
}
预期输出:
答案 0 :(得分:2)
请看一下这个答案,看看你是否需要这个或其他东西。请告诉我们,我们可能会尝试更深入地添加。
import java.awt.*;
import java.util.Random;
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 9/30/13
* Time: 6:02 PM
* To change this template use File | Settings | File Templates.
*/
public class UTBInCheck {
private JPanel contentPane;
private JPanel topLeftPanel;
private GridBagConstraints gbc;
private JTextArea infoArea;
private JScrollPane infoScroller;
private JTextField payOrderField;
private JTextField amountField;
private JButton orderSubmitButton;
private JButton orderClearButton;
private JPanel topRightPanel;
private JTextArea recentTransArea;
private JScrollPane recentTransScroller;
private JButton updateButton;
private JPanel bottomLeftPanel;
private JTextField atmWithdrawalField;
private JTextField atmDepositField;
private JTextField directDepositField;
private JTextField wireTransferField;
private JTextField electronicBillField;
private JTextField bankFeeField;
private JButton etSubmitButton;
private JButton etClearButton;
private JPanel bottomRightPanel;
private Random random;
private static final int GAP = 5;
private int checkNumber;
public UTBInCheck() {
random = new Random();
gbc = new GridBagConstraints();
gbc.insets = new Insets(GAP, GAP, GAP, GAP);
checkNumber = 811;
}
private void displayGUI() {
JFrame frame = new JFrame("University of Texas at Brownsville");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = getPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
contentPane.setLayout(new GridLayout(2, 2, GAP, GAP));
topLeftPanel = getPanel();
setTopLeftPanel(topLeftPanel);
topRightPanel = getPanel();
setTopRightPanel(topRightPanel);
bottomLeftPanel = getPanel();
setBottomLeftPanel(bottomLeftPanel);
bottomRightPanel = getPanel();
contentPane.add(topLeftPanel);
contentPane.add(topRightPanel);
contentPane.add(bottomLeftPanel);
contentPane.add(bottomRightPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void setTopLeftPanel(JPanel panel) {
panel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
panel.setLayout(new BorderLayout(GAP, GAP));
JPanel topPanel = getPanel();
topPanel.setLayout(new GridBagLayout());
JLabel universityLabel = getLabel("University of Texas at Brownsville");
addComp(topPanel, universityLabel, 0, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHWEST);
JLabel checkNumberLabel = getLabel("Check Number : " + checkNumber);
addComp(topPanel, checkNumberLabel, 1, 0, 1, 1, 0.5, 0.2,
GridBagConstraints.HORIZONTAL, GridBagConstraints.NORTHEAST);
infoArea = new JTextArea(5, 10);
infoArea.setLineWrap(true);
infoArea.setWrapStyleWord(true);
infoScroller = new JScrollPane();
infoScroller.setViewportView(infoArea);
addComp(topPanel, infoScroller, 0, 1, 2, 1, 1.0, 0.4,
GridBagConstraints.BOTH, GridBagConstraints.WEST);
JPanel centerPanel = getPanel();
centerPanel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
centerPanel.setLayout(new GridLayout(0, 2, GAP, GAP));
JLabel payOrderLabel = getLabel("Pay to the order of : ");
payOrderField = new JTextField(10);
JLabel amountLabel = getLabel("Amount : ");
amountField = new JTextField(10);
JLabel chrisBankLabel = getLabel("Cris' Bank");
JPanel buttonPanel = getPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
orderSubmitButton = new JButton("Submit");
orderClearButton = new JButton("Clear");
buttonPanel.add(orderSubmitButton);
buttonPanel.add(orderClearButton);
centerPanel.add(payOrderLabel);
centerPanel.add(payOrderField);
centerPanel.add(amountLabel);
centerPanel.add(amountField);
centerPanel.add(chrisBankLabel);
centerPanel.add(buttonPanel);
addComp(topPanel, centerPanel, 0, 2, 2, 1, 1.0, 0.4,
GridBagConstraints.BOTH, GridBagConstraints.SOUTHWEST);
panel.add(topPanel, BorderLayout.CENTER);
}
private void addComp(JPanel panel, JComponent comp,
int gridx, int gridy, int gridwidth, int gridheight,
double weightx, double weighty, int fill, int anchor) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
panel.add(comp, gbc);
}
private void setTopRightPanel(JPanel panel) {
panel.setBorder(BorderFactory.createTitledBorder("Recent Transactions"));
panel.setLayout(new BorderLayout(GAP, GAP));
recentTransArea = new JTextArea(10, 10);
recentTransArea.setLineWrap(true);
recentTransArea.setWrapStyleWord(true);
recentTransScroller = new JScrollPane();
recentTransScroller.setViewportView(recentTransArea);
JPanel buttonPanel = getPanel();
updateButton = new JButton("Update");
buttonPanel.add(updateButton);
panel.add(recentTransScroller, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.PAGE_END);
}
private void setBottomLeftPanel(JPanel panel) {
panel.setBorder(
BorderFactory.createTitledBorder("Electronic Transactions"));
panel.setLayout(new GridLayout(0, 2, GAP, GAP));
JLabel atmWithdrawLabel = getLabel("ATM Withdrawals : ");
atmWithdrawalField = new JTextField(10);
JLabel atmdepositLabel = getLabel("ATM Deposit : ");
atmDepositField = new JTextField(10);
JLabel directDepositLabel = getLabel("Direct Deposit : ");
directDepositField = new JTextField(10);
JLabel wireTransferLabel = getLabel("Wire Transfers : ");
wireTransferField = new JTextField(10);
JLabel electronicBillLabel = getLabel("Electronic Bills : ");
electronicBillField = new JTextField(10);
JLabel bankFeeLabel = getLabel("Bank Fees : ");
bankFeeField = new JTextField(10);
JPanel buttonPanel = getPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
etSubmitButton = new JButton("Submit");
etClearButton = new JButton("Clear");
buttonPanel.add(etSubmitButton);
buttonPanel.add(etClearButton);
panel.add(atmWithdrawLabel);
panel.add(atmWithdrawalField);
panel.add(atmdepositLabel);
panel.add(atmDepositField);
panel.add(directDepositLabel);
panel.add(directDepositField);
panel.add(wireTransferLabel);
panel.add(wireTransferField);
panel.add(electronicBillLabel);
panel.add(electronicBillField);
panel.add(bankFeeLabel);
panel.add(bankFeeField);
//Adding JPanel just to occupy space;
panel.add(getPanel());
panel.add(buttonPanel);
}
private JLabel getLabel(String text) {
return (new JLabel(text, JLabel.LEFT));
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(getRandomColor());
/*panel.setBorder(
BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));*/
return panel;
}
private Color getRandomColor() {
return (new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new UTBInCheck().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
输出: