任何身体请帮助我,如何JFrame在Mac 上居中.OS X?
我试过了:
this.setLocationRelativeto(null);
this.setLocationRelativeto(this);
this.setLocationRelativeto(getRootPane());
Run Code Online (Sandbox Code Playgroud)
..和
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - this.getWidth()) / 2;
final int y = (screenSize.height - this.getHeight()) / 2;
this.setLocation(x, y);
Run Code Online (Sandbox Code Playgroud)
以上都没有工作,我的框架仍然在底部并隐藏在Mac底座后面.
我的问题很简单,我猜...我想在运行程序时让我的java框架居中.
我使用了以下代码:
setLocationRelativeTo(null);
Run Code Online (Sandbox Code Playgroud)
问题:
这是框架的左上角,它居中但不是整个框架.我怎么能纠正这个问题,让整个框架居中?
谢谢您的帮助!
我注意到我的大部分摇摆GUI都会在它们居中之前闪烁.这几乎发生在我所制作的任何JFrame上,我想知道是否有解决方法.我通常所说的setVisible(true),然后pack(),其次是setLocationRelativeTo(null).这使它出现在右上角,然后自我居中.我知道闪烁正在发生,因为每个方法调用都需要时间,但是有没有解决方法(为了使它变得美观和流畅)?
在Eclipse中,当我运行代码时,这有效:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("test viewing images");
frame.setSize(600,300);
frame.setLocationRelativeTo(null); // centered on monitor
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/**
* Menu Bar stuff
*/
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
// MENU BAR
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
menuBar.setVisible(true);
// MENU 1
menu = new JMenu("File");
menuBar.add(menu);
// MENU 1 ITEM
ImageIcon icon = new ImageIcon("src/Action-exit-icon.png");
menuItem = new JMenuItem("Exit Program", icon);
menu.add(menuItem); …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,当按下同名按钮时,会创建一个新窗口(让我们称之为Cuprins).我遇到的问题是,当我关闭新窗口时,它也会关闭主窗口.关闭Cuprins窗口时,有没有关闭主窗口?
当我执行这段代码时,会弹出一个小窗口,它的内部约为 116x63,整个尺寸(包括边框)约为 140x100。如何将内部设置为我需要的样子?
public static void graphics() {
JFrame frame = new JFrame();
String title = "test window";
frame.setTitle(title);
frame.setSize(gridRow, gridCol); //101 x 101
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud) 免责声明:我是Java的新手,但我已经构建了13年的.NET应用程序.
我正在尝试构建这个Java应用程序,它为辅导做一些基本的计算.老实说,这不是一个大项目,但我甚至无法进入该Hello, World!州!我有一个要求让它变得困难:
GUI应该使用javax.swing组件jButton,jLabel,jTextField,jTextArea,jFrame和jPanel构建.
因此,我下载了NetBeans 7.3并创建了一个JavaFX in Swing应用程序.默认代码显然有效,但它使用Button而不是JButton:
private void createScene() {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
fxContainer.setScene(new Scene(root));
}
Run Code Online (Sandbox Code Playgroud)
所以当我改变它以使用JButton我必须也改变它的类型root.当我把头撞到墙上的时候,我发现了一个例子(不是直接相关的)使用了这个JRootPane,我认为这可能代替了StackPane.所以我重构了这样的代码:
private void createScene() {
JButton btn = new JButton();
btn.setText("Say 'Hello World'");
JRootPane root = new JRootPane();
root.getContentPane().add(btn); …Run Code Online (Sandbox Code Playgroud) 我有一个GUI类Gui:
public class Gui extends JFrame implements Runnable
{
private JPanel outer, inner;
private JLabel[][] labels = new JLabel[22][12];
private Color[][] defaultMap, map;
Thread t;
private int row, col;
private Color color;
public Gui()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
setBackground(Color.BLACK);
setSize(1000, 1000);
setLocation(300, 0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
defaultMap = createMap();
draw(defaultMap);
}
public Color[][] createMap()
{
Color[][] map = new Color[22][12];
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 12; …Run Code Online (Sandbox Code Playgroud) 我有一个JComboBox有12个不同的选择,根据选择的内容,我希望问题(JLabel)更改匹配选择.我已经尝试了一个if语句来查看所选内容以及它是否与应该选择的内容相匹配,然后问题会相应地改变,但JLabel在某种情况下从未真正改变过.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
public Window(){
super("Area Finder v1.0");
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("images/areafinder.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);
setSize(400, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel Instr = new JLabel("What would kind of area would you like to find?");
String[] areaChoices = {"Circle", "Square", "Rectangle", "Triangle", "Trapezoid", "Parallelogram", …Run Code Online (Sandbox Code Playgroud) 我有一个GUI看起来如下.

我想动态添加/删除面板.我ArrayList用来保持JPanel物体的痕迹.现在我可以动态添加面板,但是当我想删除面板时,我无法获取其属性,因此我无法将其删除.
这是我的代码:
public class Main implements ActionListener{
private List <myPanel> mpList;
private JPanel btnPanel;
private JButton jbtAdd,jbtDelete;
private JFrame jf;
private JPanel jp;
private JScrollPane js;
private myPanel mp;
private static int size=0;
private int selectedId=-1;
//private
public Main(){
mpList=new ArrayList<myPanel>();
btnPanel=new JPanel();
jbtAdd=new JButton("addJpanel");
jbtDelete=new JButton("delJpanel");
btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
btnPanel.add(jbtAdd);
btnPanel.add(jbtDelete);
jf=new JFrame("hello");
jp=new JPanel();
js=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//jp.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
jf.setLayout(new BorderLayout(10,10));
jp.setLayout(new GridLayout(0,1,1,1));
jp.setPreferredSize(new Dimension(500, 82*6));
for(int i=0;i<6;i++){
myPanel mp=new myPanel();
//mp.setSize(400, 82);
//mp.setBounds(0,82*i,480,82); …Run Code Online (Sandbox Code Playgroud)