这是一个 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 中。
其他建议:
- 避免使用 null 布局和
setBounds
,因为这会导致 GUI 僵化,无法适应不同的操作系统或用途。学习和使用布局管理器会更好。 - 尝试尽可能地将模型代码(程序的非 GUI 逻辑部分)与 View (GUI 部分)分开。这将使实现这些事情变得更容易,并使调试和测试代码变得更容易。
- 与手动删除和更换组件相比,使用 CardLayout 交换组件要好得多。这使得交换变得更加容易和安全
在审查您的代码后,我的建议发生了变化。看起来您想要创建输入表单来对点执行转换,并且想要根据 JComboBox 中所做的选择来更改转换类型。如果是这样,我的建议是:
- 可能不需要创建多个相似的 JPanel 并交换 JPanel
- 而是使用一个 JPanel 来完成整个事情
- 给它一个 JButton,标题为“提交”
- 为 JButton 提供一个 ActionListner
- 在该监听器中,检查 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;
}
}
关于java - 如何制作 JPanel 的副本,其中所有 j组件都具有与原始面板相同的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61271121/