-3
jframeでjdialogを中央に配置できません。私はこれをしようとしましたが、うまくいかない:jframeでjdialogを中央に配置できません
public void open(){
this.setVisible(true);
this.setLocationRelativeTo(Main.instance.frame);
}
コードが必要な場合は教えてください!ご協力いただきありがとうございます!
メイン:
public class Main {
public static Main instance;
public JXFrame frame;
public JXPanel panel;
public String name = "";
// Windows
public WindowCreateProject windowCreateProject;
public static void main(String[] args) {
instance = new Main();
instance.start();
}
public void init() {
// Init components
Icons.createIcons();
MainMenu.init();
// JXFrame
frame = new JXFrame();
panel = new JXPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setLayout(new CardLayout());
frame.setPreferredSize(new Dimension(1980, 1080));
frame.setDefaultCloseOperation(JXFrame.EXIT_ON_CLOSE);
// Add components
frame.add(panel);
panel.add(MainMenu.menuBar);
// JXFrame
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle(name);
// Create windows
windowCreateProject = new WindowCreateProject();
update();
}
public void update(){
while(frame.isActive()){
}
}
public void start() {
init();
}
public void stop() {
System.exit(0);
}
}
WindowCreateProject:
public class WindowCreateProject extends Window {
private static final long serialVersionUID = 1L;
public JTextField nameField;
public JButton createProject;
public JButton cancel;
public WindowCreateProject() {
super("Create New Project");
// Setup components
nameField = new JTextField();
nameField.setSize(100, nameField.getHeight());
createProject = new JButton("Create Project", Icons.ok);
cancel = new JButton("Cancel", Icons.cancel);
// Add components
panel.add(nameField);
panel.add(createProject);
panel.add(cancel);
addActionListeners();
}
public void addActionListeners(){
cancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
close();
}
});
}
public void close(){
nameField.setText("");
this.setVisible(false);
}
}
ウィンドウ:
public class Window extends JDialog {
private static final long serialVersionUID = 1L;
public JPanel panel;
public Window(String title) {
panel = new JPanel();
this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
this.setLayout(new GridLayout());
this.setResizable(false);
this.setPreferredSize(new Dimension(300, 400));
this.add(panel);
this.pack();
this.setTitle(title);
}
public void open(){
this.setVisible(true);
this.setLocationRelativeTo(Main.instance.frame);
}
}
MenuMain: パブリッククラスメインメニュー{
public static JMenuBar menuBar;
public static JMenu menuProjects;
public static JMenu menuOptions;
public static JMenu menuHelp;
// Projects menu items
public static JMenuItem createProject;
public static JMenuItem myProjects;
public static JMenuItem saveProject;
public static JMenuItem saveProjectAs;
public static JMenuItem exportResourcepackTo;
public static JMenuItem exportResourcepackToMinecraft;
public static JMenuItem exit;
public static void init(){
// Init menu
menuBar = new JMenuBar();
menuBar.setLayout(new FlowLayout(FlowLayout.LEFT));
menuProjects = new JMenu("Projects");
menuOptions = new JMenu("Options");
menuHelp = new JMenu("Help");
createMenuItems();
// Add components
menuBar.add(menuProjects);
menuBar.add(menuOptions);
menuBar.add(menuHelp);
// Add menu items to project menu
menuProjects.add(createProject);
menuProjects.add(myProjects);
menuProjects.add(saveProject);
menuProjects.add(saveProjectAs);
menuProjects.addSeparator();
menuProjects.add(exportResourcepackTo);
menuProjects.add(exportResourcepackToMinecraft);
menuProjects.addSeparator();
menuProjects.add(exit);
createActionListeners();
}
public static void createMenuItems(){
createProject = new JMenuItem("Create project", Icons.createProject);
myProjects = new JMenuItem("My projects", Icons.myProjects);
saveProject = new JMenuItem("Save project");
saveProjectAs = new JMenuItem("Save project as");
exportResourcepackTo = new JMenuItem("Export resourcepack to");
exportResourcepackToMinecraft = new JMenuItem("Export resourcepack to minecrat");
exit = new JMenuItem("Exit", Icons.cancel);
}
public static void createActionListeners(){
// Project menu
createProject.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
Main.instance.windowCreateProject.open();
}
});
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
Main.instance.stop();
}
});
}
}
''もうコードが必要かどうか教えてください! '' - 私たちは** less **が必要です。あなたは、あなたの問題に最も関連しない、大量のコードを投稿しました。これは、不要なコードで私たちを危険にさらし、おそらく私たちがあなたの問題を見て、あなたを助けることを妨げています。 –
あなたのプログラム全体を見たいとは思っていません。特に60行を超える場合は、コンパイルして実行する最小のビットにコードを凝縮してください。問題は、あなたの問題、つまり、[最小限のサンプルプログラム](http://stackoverflow.com/help/mcve)をまだ示しています。 –
編集の回答を参照してください。 –