[英]JButton changing position after updating JTextArea
我目前正在從事一個項目,該項目應該創建一個包含電子組件(如電阻器,LED和電容器)的電路板。
我有一個主JFrame,其中包含所有小的JPanel:Centerpanel-包含板Rightpanel-包含有關所選組件的信息
我如何想象它的工作原理如下:
您從JComboBox中選擇一個組件
1.1。 組件是通過ComponentFactory創建的
之后,當您單擊該組件時,信息應顯示在JTextArea的右側面板中。
我的問題是:一切都按計划進行,直到我第二次單擊該組件以獲取有關該組件的信息(如右面板所示)。 然后,將JButton調整為較小的尺寸,並移至左上角。
當我添加代碼時發生了這種情況:
String strBuilder = "Type: " + e.getComponent().getName() + "\n" +
"ID: " + ((Components) e.getComponent()).getCompID() + "\n";
infoContainer.append(strBuilder);
完整的代碼:
public class Framework extends JFrame{
// Declaring variables for the frame
private ComponentFactory cf = new ComponentFactory();
private JToolBar menuToolbar;
private String[] menuItemsString = new String[]{ "newFile", "loadFile", "saveFile" };
private JButton[] menuItems = new JButton[menuItemsString.length];
private JPanel menuPane, centerPane, innerCenter;
private JPanel rightPane;
private JTextArea infoContainer;
private JComboBox<String> componentList;
private final String NOT_SELECTABLE_OPTION = " - Select a component - ";
private String[] componentListStrings = new String[] {"Resistor","LED","Capacitor","Inductor"};
private Components newComponent, selectedComponent;
private boolean componentSelected = false;
private ArrayList<Components> compList = new ArrayList<Components>();
/**
* Creates a new dispatcher that will listen for keyboard actions on all the selected elements.
* @author Zovsaman
*/
private class MyDispatcher implements KeyEventDispatcher {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID() == KeyEvent.KEY_PRESSED){
if(componentSelected){
if(e.getKeyCode() == 27){
componentSelected = false;
}
}
}
return false;
}
}
public Framework(){
setLayout(new BorderLayout());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize((int)screenSize.getWidth()/2, (int)screenSize.getHeight()/2);
// Initiating all the JPanels and stuff
menuToolbar = new JToolBar(null);
componentList = new JComboBox<String>();
centerPane = new JPanel();
innerCenter = new JPanel();
rightPane = new JPanel();
infoContainer = new JTextArea(5, 20);
// Setting settings and adding buttons to JToolBar at the top of the program
menuToolbar.setFloatable(false);
for(int i = 0; i < menuItemsString.length; i++){
menuItems[i] = new JButton();
menuItems[i].setName(menuItemsString[i]);
addIcon(menuItems[i]);
menuToolbar.add(menuItems[i]);
if(i < menuItemsString.length){
// Add spacing to the button menu
menuToolbar.addSeparator(new Dimension(4, 0));
}
}
// Changing settings on the JComboBox that holds all the different kinds of components
// Changing the ComboBox to a fixed Size
componentList.setMaximumSize(new Dimension(200, 24));
// Adding all the items to JComboBox
componentList.addItem(NOT_SELECTABLE_OPTION);
for(int i = 0; i < componentListStrings.length; i++){
componentList.addItem(componentListStrings[i]);
}
// Setting actionListener to listen after changing the JComboBox
componentList.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
componentSelected = true;
newComponent = cf.createComponent((String)componentList.getSelectedItem());
}
});
menuToolbar.add(componentList);
add(menuToolbar, BorderLayout.NORTH);
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
// Creating the center piece
innerCenter.setLayout(new BoxLayout(innerCenter, EXIT_ON_CLOSE));
innerCenter.setPreferredSize(new Dimension(700, 400));
innerCenter.setBackground(Color.LIGHT_GRAY);
innerCenter.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(newComponent != null){
Dimension size = newComponent.getPreferredSize();
newComponent.setBounds(e.getX() - (size.width/2), e.getY() - (size.height/2), size.width, size.height);
newComponent.setLocation(e.getPoint());
newComponent.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
// TODO Update infopanel with info about marked component....
String strBuilder = "Type: " + e.getComponent().getName() + "\n" +
"ID: " + ((Components) e.getComponent()).getCompID() + "\n";
infoContainer.append(strBuilder);
}
});
innerCenter.add(newComponent);
innerCenter.repaint();
componentSelected = false;
newComponent = null;
}
}
});
centerPane.add(innerCenter);
centerPane.setVisible(true);
add(centerPane, BorderLayout.CENTER);
JPanel tempPane = new JPanel();
tempPane.setLayout(new BoxLayout(tempPane, BoxLayout.PAGE_AXIS));
// Right pane, info panel
// rightPane = new JPanel();
// infoContainer = new JTextArea();
infoContainer.setBackground(Color.LIGHT_GRAY);
infoContainer.setVisible(true);
JLabel tempLabel = new JLabel("Information about component:");
tempPane.add(tempLabel);
tempPane.add(infoContainer);
rightPane.add(tempPane);
add(rightPane, BorderLayout.EAST);
setVisible(true);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
和描述我的意思更好的圖像(第一個圖像:將組件放在onclick板上,第二個圖像:再次單擊該組件以從中獲取數據以顯示在右側面板中)
添加組件:
再次單擊它:
您在混淆Swing布局。 您正在使用layoutmanager設置面板:
innerCenter.setLayout(new BoxLayout(innerCenter, EXIT_ON_CLOSE));
但是隨后,您嘗試在其上設置組件的邊界,就像您手動放置它一樣:
newComponent.setBounds(e.getX() - (size.width/2), e.getY() - (size.height/2), size.width, size.height);
newComponent.setLocation(e.getPoint());
如果要手動放置,則需要從面板中刪除布局管理器:
innerCenter.setLayout(null);
這樣您就可以告訴Swing您將手動設置所有子項的邊界和位置。 否則,每當您添加/刪除/重新驗證/包裝容器時,布局管理器都將嘗試重新布局所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.