我在java swing中創建了一個應用程式,并在其中制作了一個我認為叫做嵌入式Jpanel的東西,據我所知,這是一個Jpanel中的一個Jpanel。為了更簡單,我們將使用面板名稱,即內容、側邊欄和內容側邊欄
。側邊欄是帶有按鈕的應用程式的側邊欄
。內容是應用程式的主要內容
。內容側邊欄是內容中的一個側邊欄,我使用這個側邊欄使我的設定頁面除了正常的側邊欄外還有自己的側邊欄。
我讓邊欄向西對齊,而內容則向中央對齊
在content.add(contentSidebar, BorderLayout.WEST);之后,它不會使內容側邊欄向西移動,我不確定原因。
這是我的源代碼
package Assets.Settings;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Security implements ActionListener{
JFrame安全。
JPanel sideBar;
JPanel的內容。
//for sidebar
JB按鈕的音量。
JB按鈕的安全性。
JButton contactUs;
JPanel contentSidePanel。
//JButton twoStep;
//JButton changePassword;
public void security(){
Security = new JFrame();
sideBar = new JPanel();
content = new JPanel();
contentSidePanel = new JPanel();
sideBar.setPreferredSize(new Dimension(125, 700) 。)
content.setPreferredSize(new Dimension(1000, 700) )。
content.setBackground(Color.YELLOW)。
Security.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
Security.setTitle("Anonyomail Security Settings")。
Security.setSize(1152, 700)。
Security.setLayout(new java.awt.BorderLayout())。
contentSidePanel.setLayout( new java.awt.BorderLayout())。
volume = new JButton()。
security = new JButton();
contactUs = new JButton();
// twoStep = new JButton();
// changePassword = new JButton();
volume.addActionListener(this)。
contactUs.addActionListener(this)。
volume.setText("體積")。
security.setText("安全")。
contactUs.setText("聯系我們")。
//changePassword.setText("Change Password");/span>
// twoStep.setText("2-Step");.
security.setBackground(Color.decode("#24a0ed"/span>))。
contentSidePanel.setPreferredSize(new Dimension(100, 700) )。
contentSidePanel.setBackground(Color.BLACK)。
// contentSidePanel.add(changePassword);
// contentSidePanel.add(twoStep);.
content.add(contentSidePanel, BorderLayout.WEST);
sideBar.add(volume);
sideBar.add(security);
sideBar.add(contactUs);
Security.add(sideBar, BorderLayout.WEST);
Security.add(content, BorderLayout.CENTER);
Security.pack();
Security.setVisible(true)。
}
; }
public void actionPerformed(ActionEvent e) {
if(e.getSource() == volume){
//打開音量。
}else if(e.getSource() == contactUs){
//打開聯系我們。
}
}
}
編輯
問題是我沒有給內容一個邊框布局
uj5u.com熱心網友回復:你呼叫content.add(contentSidePanel, BorderLayout.WEST)然而你從未將content的布局管理器設定為BorderLayout。我相信JPanel的默認布局管理器是FlowLayout,因此BorderLayout.WEST意味著什么。
另外,你似乎只在contentSidePanel中添加了一個東西,content。我相信BorderLayout將使content的大小與contentSidePanel相同,并且指定BorderLayout.WEST將沒有任何影響,因為中間或東邊沒有任何東西。試著在content中添加一個測驗的JPanel,并將它的布局設定為BorderLayout.CENTER,看看我是否正確。
注意:BorderLayout的位置常量在JDK 1.4中從羅盤位置(NORTH,EAST,. ...)到更多的常量(PAGE_START, PAGE_END, LINE_START, LINE_END 和 CENTER)。因此,使用BorderLayout.WEST應該被替換為BorderLayout.LINE_START。- 從如何使用BorderLayout
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/310277.html
標籤:
