我有这种方法在名为MaterialProperties的类中打印和设置Solid Object的材质属性,该类具有printMaterial& setMaterial方法。
public void Btn3_callback ( ) throws Exception {
Model model = session.GetCurrentModel();
if (model == null) {
mesg = "No Model Selected!!";
//TextField.Area("No Model Selected!!");
System.exit(0);
}
else {
Solid solid= (Solid) model;
String newMaterial="copper";//user input for new Material name
printMaterial(solid);//printMaterial print the Material properties of the Solid object
setMaterial(solid,newMaterial);//setMaterial sets the Material properties of the Solid object to the material entered
}
}
我需要获取newMaterial的用户输入而不是硬编码。我需要做的是显示所有可用的材料类型,以便用户只需选择所需的材料。所以我尝试使用JFrame来做到这一点。这是我的代码:
public class MaterialWindow {
JFrame frame = new JFrame("Material Selection");
public MaterialWindow(){
// Directory path here
String path = "W:\\materials";
JFrame frame = new JFrame("Material Selection");
JPanel panel = new JPanel(new GridLayout(0, 4));
ButtonGroup bg = new ButtonGroup();
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
JRadioButton button;
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".mtl") || files.endsWith(".MTL"))
{
button = new JRadioButton(files);
panel.add(first,BorderLayout.CENTER);
panel.revalidate();
bg.add(button);
first.addActionListener(new MyAction());
}
}
}
frame.add(panel, BorderLayout.NORTH);
frame.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
frame.setSize(1000, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String newMaterial =e.getActionCommand();
String[] split = newMaterial.split("\\.");
newMaterial = split[0];
newMaterial.trim();
//set the newMaterial for btn3_callback OR call the setMaterial method of MaterialPropeties class
frame.dispose();
}
}
}
现在问题是如何在我的Btn3_callback()函数中使用从单选按钮中选择的newMaterial字符串到newMaterial?当我在类MyAction中为newMaterial创建一个getString()方法并使用它Btn3_callback时,它总是返回null;
我有什么方法可以做到这一点?或者我可以以任何不同的方式实现这个想法? 感谢
答案 0 :(得分:4)
使用JOptionPane
代替框架。在选项窗格中放置一个列表(JList
..或JComboBox
),并为选定对象查询返回的组件(窗格已关闭)。
应在EDT上创建和更改GUI(不包括电池)。
import java.io.File;
import javax.swing.*;
public class QuickTest {
public static void main(String[] args) throws Exception {
File[] files = new File(System.getProperty("user.home")).listFiles();
JFrame f = new JFrame("Faux J-Link");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JEditorPane jep = new JEditorPane();
f.add(new JScrollPane(jep));
f.setSize(600,400);
f.setLocationByPlatform(true);
f.setVisible(true);
JComboBox choices = new JComboBox(files);
int result = JOptionPane.showConfirmDialog(f, choices);
if (result==JOptionPane.OK_OPTION) {
System.out.println("OK");
File file = files[choices.getSelectedIndex()];
jep.setPage(file.toURI().toURL());
}
}
}