我如何制作一个JPanel的副本,它的所有jcomponent都具有与原始面板相同的功能



这是一个gui转换项目。我有一个jcombobox,它更新我的jpanel jpTransDetails,在选择相应的jbutton时执行翻译、旋转等。现在,我必须在另一个部分工作,我希望有一个与此功能相同的jcomboBox和JPanel转换细节的精确副本。实现这一目标的最佳方式是什么?

请注意,我只向您展示了代码的一部分,以免太长。对于其他变换,重复这些部分。

更新:我已经尝试创建一个使用类向jpanel添加新jpanel的基本示例,但它不起的作用

transNameList = new String[] {"Translation","Rotation","Scaling","Shear","Reflection"};
jcbTranslations = new JComboBox(transNameList);
mPane.add(jcbTranslations); //mPane is my main jpanel
//transDetails JPANEL
jpTransDetails = new JPanel(null);
jpTransDetails.setBounds(10,95,320,103);
jpTransDetails.setOpaque(false);
Border border = BorderFactory.createLineBorder(Color.white);
jpTransDetails.setBorder(border);

//TRANSLATION
JLabel lblTx = new JLabel("Enter X-Coordinate: ");
lblTx.setForeground(Color.BLACK);
lblTx.setFont(new Font("Segoe UI", Font.PLAIN, 12));
lblTx.setBounds(10,6,150,12);
lblTx.setToolTipText("Number of units moved along x-axis");
tTx = new JTextField();
tTx.setText("0");
tTx.setColumns(10);
tTx.setBounds(175, 4, 50, 18);
JLabel lblTy = new JLabel("Enter Y-Coordinate: ");
lblTy.setForeground(Color.black);
lblTy.setFont(new Font("Segoe UI", Font.PLAIN, 12));
lblTy.setBounds(10, 60, 150, 12);
lblTy.setToolTipText("Number of units moved along y-axis");

tTy = new JTextField();
tTy.setText("0");
tTy.setColumns(10);
tTy.setBounds(175, 60, 50, 18);

bTranslate = new JButton("TRANSLATE");
bTranslate.setBounds(210,1,110, 25);
bTranslate.setBackground(Color.black);
bTranslate.setForeground(Color.WHITE);
//JPANEL FOR TRANSFORMATION BUTTON
jpTransBtn =new JPanel(null);
jpTransBtn.setBounds(10,200,320,28);
jpTransBtn.setOpaque(false);
jpTransDetails.setBounds(10,95,320,100);

//ADDING FOR TRANSLATION 
jpTransDetails.add(lblTx);
jpTransDetails.add(tTx);
jpTransDetails.add(lblTy);
jpTransDetails.add(tTy);
//ADDING TRANSLATION BUTTON
jpTransBtn.add(bTranslate);
mPane.add(jpTransDetails);
mPane.add(jpTransBtn);

//ITEM LISTENER FOR jcbTranslations
jcbTranslations.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
{
if (jcbTranslations.getSelectedIndex()==0) //TRANSLATION
{
jpTransDetails.removeAll();
jpTransDetails.repaint();
jpTransBtn.removeAll();
jpTransBtn.repaint();
//ADDING FOR TRANSLATION 
jpTransDetails.add(lblTx);
jpTransDetails.add(tTx);
jpTransDetails.add(lblTy);
jpTransDetails.add(tTy);
//ADDING TRANSLATION BUTTON
jpTransBtn.add(bTranslate);

}
if (jcbTranslations.getSelectedIndex()==1) //ROTATION
{
jpTransDetails.removeAll();
jpTransDetails.repaint();
jpTransBtn.removeAll();
jpTransBtn.repaint();
//ADDING FOR ROTATION
jpTransDetails.add(lblRx);
jpTransDetails.add(Rotx);
jpTransDetails.add(lblRy);
jpTransDetails.add(Roty);
jpTransDetails.add(lblAngx);
jpTransDetails.add(tAngle);

//ADDING ROTATION BUTTON
jpTransBtn.add(bRotate);

}

更新

public class MainPanelClass extends JFrame   
{
public static JPanel selectionpanel;
MainPanelClass()
{
selectionpanel = new JPanel();
selectionpanel.setPreferredSize(new Dimension(100,200));
selectionpanel.setBackground(Color.BLUE);
selectionpanel.add(new CompositeTransformationsPanel());
add(selectionpanel);
}
public static void main(String[] args)
{
MainPanelClass mpc = new MainPanelClass();
mpc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mpc.setVisible(true);
mpc.setSize(300,400);
}

}

This is my class extending a jpanel
public class CompositeTransformationsPanel extends JPanel
{
CompositeTransformationsPanel()
{
setPreferredSize(new Dimension(20,10));
setBackground(Color.red);
setOpaque(false);
}
}

最好的方法是创建一个扩展JPanel或通过组合包含JPanel的类,其工作是每次都以完全相同的方式创建感兴趣的组件。如果你需要两者共享模型,那么你可以给这个类一个复制构造函数,它从所有组件中提取模型,并将这些模型放在新创建的JPanel中。

其他建议:

  • 避免空布局和setBounds,因为这会导致僵化的GUI无法适应不同的操作系统或用途。更好地学习和使用布局管理器
  • 尽量将模型代码(程序的非GUI逻辑部分(与视图(GUI部分(分离。这将使实现这些事情变得更容易,并使调试和测试代码变得更容易
  • 与其手动移除和更换组件,不如使用CardLayout交换组件,这样交换更容易、更安全

在审查您的代码时,我的建议发生了变化。看起来您想要创建用于对点执行变换的输入表单,并且您想要根据JComboBox中的选择来更改变换的类型。如果是这样,我的建议是:

  • 可能不需要创建多个类似的JPanel和交换JPanel
  • 相反,使用一个JPanel来处理整个事情
  • 给它一个JButton,比如标题为"提交">
  • 给那个JButton一个ActionLister
  • 在该侦听器中,检查JComboBox的当前状态,即它所选的项
  • 根据所选项目进行适当的计算

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.Border;
@SuppressWarnings("serial")
public class PointManipulationPanel extends JPanel {
private static final int GAP = 5;
private JComboBox<Transform> transformCombo = new JComboBox<Transform>(Transform.values());
private JTextField xCoordField = new JTextField("0", 5);
private JTextField yCoordField = new JTextField("0", 5);
public PointManipulationPanel() {   
JPanel coordinatePanel = new JPanel(new GridBagLayout());
Border outsideBorder = BorderFactory.createLineBorder(Color.WHITE);
Border insideBorder = BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP);
Border border = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
coordinatePanel.setBorder(border);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(GAP, GAP, GAP, GAP);
coordinatePanel.add(new JLabel("Enter X-Coordinate: "), gbc);
gbc.gridy = 1;
coordinatePanel.add(new JLabel("Enter Y-Coordinate: "), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
coordinatePanel.add(xCoordField, gbc);
gbc.gridy = 1;
coordinatePanel.add(yCoordField, gbc);
JPanel topPane = new JPanel();
topPane.add(transformCombo);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(e -> submitActionPerformed(e));
JPanel bottomPanel = new JPanel();
bottomPanel.add(submitButton);
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(topPane, BorderLayout.PAGE_START);
add(coordinatePanel);
add(bottomPanel, BorderLayout.PAGE_END);
}
private void submitActionPerformed(ActionEvent e) {
int x = 0;
int y = 0;
try {
x = Integer.parseInt(xCoordField.getText());
y = Integer.parseInt(yCoordField.getText());
} catch (NumberFormatException nfe) {
// warn user with JOptionPane that data entered no goo
String message = "Invalid number entry";
String title = "Invalid Data";
int messageType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(PointManipulationPanel.this, message, title, messageType);
// exit method
return;
}
// use x and y here in the calculations
Transform transform = (Transform) transformCombo.getSelectedItem();
switch (transform) {
case TRANSLATION:
// TODO: real code to do calculations goes here
System.out.printf("Do translation here on x and y: [%d, %d]%n", x, y);
break;
case ROTATION:
// TODO: real code to do calculations goes here
System.out.printf("Do rotation here on x and y: [%d, %d]%n", x, y);
break;
case SCALING:
// TODO: real code to do calculations goes here
System.out.printf("Do scaling here on x and y: [%d, %d]%n", x, y);
break;
case SHEAR:
// TODO: real code to do calculations goes here
System.out.printf("Do shear here on x and y: [%d, %d]%n", x, y);
break;
case REFLECTION:
// TODO: real code to do calculations goes here
System.out.printf("Do reflection here on x and y: [%d, %d]%n", x, y);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + transform);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
PointManipulationPanel mainPanel = new PointManipulationPanel();
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
enum Transform {
TRANSLATION("Translation"), ROTATION("Rotation"), SCALING("Scaling"), SHEAR("Shear"), REFLECTION("Reflection");
private String name;
private Transform(String name) {
this.name = name;
}
String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}

最新更新