使用卡布局時,我有包功能問題。 我創建了1個包含JPanel的JFrame(在cardlayout中),這個JPanel包含兩個JPanel。 ...所以當我運行我的程序時,窗口被調整爲程序中最大的JPanel,我不能動態調整它的大小。Java Swing重新包裝組件
現在屏幕: 我background1(小背景)班時,我只能用這個面板運行的程序有寬度像200 http://i41.tinypic.com/scfi88.jpg 當我跑我的添加background2背景得到它的寬度程序,所以它看起來像400 http://i41.tinypic.com/2lu742x.jpg 所以它得到我background2面板
在這兩個背景類我有按鈕,返回到預覽背景的寬度,但它永遠不會調整。我已經在thoose動作按鈕上使用了revalidate和repaint函數。 代碼: 主類和 background1和 background2(背景代碼大,因爲它們是由NetBeans的設計模式中生成(它們只是幾個空組件):
public class main extends javax.swing.JFrame {
private JPanel mainPanel;
public main() {
mainPanel = new JPanel(new CardLayout());
add(mainPanel);
Background2 card1 = new Background2(mainPanel);
Background1 card2 = new Background1(mainPanel);
mainPanel.add(card1,"card1");
mainPanel.add(card2,"card2");
CardLayout cl = (CardLayout) (mainPanel.getLayout());
cl.show(mainPanel, "card1");
mainPanel.revalidate();
mainPanel.repaint();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new main().setVisible(true);
}
});
}
}
public class Background1 extends javax.swing.JPanel {
private JPanel mainPanel;
/**
* Creates new form HomePanel
*/
public Background1(JPanel panel) {
mainPanel=panel;
initComponents();
}
public JPanel getMainPanel() {
return mainPanel;
}
public void setMainPanel(JPanel mainPanel) {
this.mainPanel = mainPanel;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
left_Panel = new javax.swing.JPanel();
zakladka1_Button = new javax.swing.JButton();
background_Panel = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
powrot_Button = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
jTable3 = new javax.swing.JTable();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
zakladka1_Button.setText("button");
zakladka1_Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zakladka1_ButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout left_PanelLayout = new javax.swing.GroupLayout(left_Panel);
left_Panel.setLayout(left_PanelLayout);
left_PanelLayout.setHorizontalGroup(
left_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(left_PanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(zakladka1_Button)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
left_PanelLayout.setVerticalGroup(
left_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(left_PanelLayout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(zakladka1_Button)
.addGap(433, 433, 433))
);
jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable2);
javax.swing.GroupLayout background_PanelLayout = new javax.swing.GroupLayout(background_Panel);
background_Panel.setLayout(background_PanelLayout);
background_PanelLayout.setHorizontalGroup(
background_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(background_PanelLayout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(83, Short.MAX_VALUE))
);
background_PanelLayout.setVerticalGroup(
background_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, background_PanelLayout.createSequentialGroup()
.addContainerGap(14, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
powrot_Button.setText("<--");
powrot_Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
powrot_ButtonActionPerformed(evt);
}
});
jLabel1.setText("jLabel1");
jTable3.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane3.setViewportView(jTable3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(left_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(powrot_Button)))
.addGap(18, 18, 18)
.addComponent(background_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(powrot_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(left_Panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(background_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(115, 115, 115))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42))))
);
}// </editor-fold>
private void powrot_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout cl = (CardLayout) (mainPanel.getLayout());
cl.show(mainPanel, "card1");
mainPanel.revalidate();
mainPanel.repaint();
}
private void zakladka1_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
// Variables declaration - do not modify
private javax.swing.JPanel background_Panel;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTable jTable3;
private javax.swing.JPanel left_Panel;
private javax.swing.JButton powrot_Button;
private javax.swing.JButton zakladka1_Button;
// End of variables declaration
}
public class Background2 extends javax.swing.JPanel {
private JPanel mainPanel;
public Background2(JPanel panel) {
mainPanel=panel;
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
loginFieldLabel = new javax.swing.JLabel();
zalogujButton = new javax.swing.JButton();
loginField = new javax.swing.JTextField();
passwordField = new javax.swing.JPasswordField();
logowanieLabel = new javax.swing.JLabel();
passwordFieldLabel = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
loginFieldLabel.setText("Login");
zalogujButton.setText("Zaloguj");
zalogujButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zalogujButtonActionPerformed(evt);
}
});
loginField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginFieldActionPerformed(evt);
}
});
passwordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordFieldActionPerformed(evt);
}
});
logowanieLabel.setText("Logowanie");
passwordFieldLabel.setText("Hasło");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(zalogujButton)
.add(149, 149, 149))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(logowanieLabel)
.add(layout.createSequentialGroup()
.add(56, 56, 56)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(passwordFieldLabel)
.add(loginFieldLabel))
.add(26, 26, 26)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(loginField)
.add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 193, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(105, 105, 105)
.add(jButton1)))
.addContainerGap(190, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(logowanieLabel)
.add(29, 29, 29)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(loginField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(loginFieldLabel))
.add(28, 28, 28)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(passwordFieldLabel)
.add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(33, 33, 33)
.add(zalogujButton)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton1)
.addContainerGap(101, Short.MAX_VALUE))
);
}// </editor-fold>
private void zalogujButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void loginFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout cl = (CardLayout) (mainPanel.getLayout());
cl.show(mainPanel, "card2");
mainPanel.revalidate();
mainPanel.repaint();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField loginField;
private javax.swing.JLabel loginFieldLabel;
private javax.swing.JLabel logowanieLabel;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordFieldLabel;
private javax.swing.JButton zalogujButton;
// End of variables declaration
}
編輯:
CardLayout cl = (CardLayout) (mainPanel.getLayout());
cl.show(mainPanel, "card1");
l.preferredLayoutSize(mainPanel);//l = ShowingSizeCardLayout i added here prints on dim.width and dim.height
mainPanel.revalidate();
輸出:
width: 509
height: 323
width: 1203
height: 537
但它總是509 323.
爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 (一個源文件,而不是3.)BTW - PNG比截圖更好的JPG ..請參閱[如何創建屏幕截圖?](http://meta.stackexchange.com/questions/99734/how-doi-i-創建一個截圖來說明一個職位)(關於製作*很棒*截圖的提示)。 –
好的,我修復了一點點代碼。關於圖片我沒有足夠的聲譽來使用該功能。 Thx的建議。 – user2771738
*「關於圖片我沒有足夠的聲譽來使用該功能」*我給的鏈接幾乎與發佈圖片到網站無關,*所有*與*製作* *可以上傳到這個偉大的圖像網站,任何其他圖像網站,或用於記錄程序本身。請再次閱讀,這次更仔細。至於將圖片上傳到* this *網站(SO),如果它們不是JPEG,它們的字節就會更小,我會爲你做到這一點。 –