- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于某种原因,我的面板没有显示边框,我不确定为什么,是否缺少某些内容?
我有一个主类,它运行框架类以及独立于 GUI 的其他类
这是我的框架类的代码:
import javax.swing.*;
import java.awt.*;
public class Frame
{
public static int xsize;
public static int ysize;
public static void main()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new MainFrame("Warlock of Firetop Mountain");
//Implementing Toolkit to allow computer to get dimensions of screen and assign them to two int values
Toolkit tk = Toolkit.getDefaultToolkit();
Frame.xsize = (int) tk.getScreenSize().getWidth();
Frame.ysize = (int) tk.getScreenSize().getHeight();
frame.setTitle("Warlock of Firetop Mountain");
frame.setSize(new Dimension(xsize, ysize));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
frame.java 从 MainFrame.java 获取其面板:
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
private Panel1 storyPanel;
private Panel2 statsPanel;
private Panel3 commandsPanel;
public MainFrame(String title)
{
super(title);
// Setting Layout
setLayout(new BorderLayout());
storyPanel = new Panel1();
statsPanel = new Panel2();
commandsPanel = new Panel3();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(statsPanel, BorderLayout.EAST);
p.add(commandsPanel, BorderLayout.SOUTH);
}
}
这会调用我的三个面板,如下所示:
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Dimension;
import java.awt.Color;
public class Panel1 extends JPanel
{
public Panel1()
{
//Set size of Panel1
int xsizeP1 = (Frame.xsize / 2);
int ysizeP1 = (Frame.ysize / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
}
}
当代码运行时,窗口以全屏方式启动,但没有边框或可能的面板可见。
感谢您的帮助,抱歉,如果我的问题很乏味,我对编程还比较陌生。
这大致就是我希望面板的样子,最终将组件添加到面板并使用 GridBagConstraints 来控制格式
最佳答案
// this creates the JPanels and sets their preferred sizes
JFrame frame = new MainFrame("Warlock of Firetop Mountain");
//this sets your size static contents -- after the above's been done!
Toolkit tk = Toolkit.getDefaultToolkit();
Frame.xsize = (int) tk.getScreenSize().getWidth();
Frame.ysize = (int) tk.getScreenSize().getHeight();
您将所有 JPanel 的首选尺寸设置为 0、0,因此您看不到任何边框。您的尺寸调整是在您创建 JPanel 后创建的,这种尺寸调整方法对我来说看起来很危险。
<小时/>好的,感谢您发布所需 GUI 的图像。我的建议是:
例如,像这样:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class TomGuiPanel extends JPanel {
// rows and cols for jtextarea
private static final int CURRENT_AREA_ROWS = 20;
private static final int CURRENT_AREA_COLS = 40;
// columns for command jtextfied
private static final int COMMANDS_FIELD_COLS = 50;
// size of GUI component gaps
private static final int EB_GAP = 3;
private static final int NUMBER_OF_OPTIONS = 5;
// number if ImageIcons displayed within the user image char JList
private static final int CHAR_IMG_VISIBLE_ROWS = 5;
// a guess of the width of the largest image icon in the JList
// You'd use a different number
private static final int USER_IMG_CHAR_IMG_WIDTH = 70;
private JTextArea currentTextArea = new JTextArea(CURRENT_AREA_ROWS, CURRENT_AREA_COLS);
private JTextField commandsField = new JTextField(COMMANDS_FIELD_COLS);
private EnterAction enterAction = new EnterAction("Enter");
private DefaultListModel<Icon> charImgListModel = new DefaultListModel<>();
private JList<Icon> charImgList = new JList<>(charImgListModel);
public TomGuiPanel() {
JPanel topBtnPanel = new JPanel(new GridLayout(1, 0, EB_GAP, EB_GAP));
String[] btnTexts = { "Inventory", "Options", "Save", "Load" };
for (String txt : btnTexts) {
topBtnPanel.add(new JButton(txt));
}
JPanel characteristicsPanel = new JPanel(new GridBagLayout());
addCharacteristics(characteristicsPanel, "HP", 20, 0);
addCharacteristics(characteristicsPanel, "Attack", 12, 1);
addCharacteristics(characteristicsPanel, "Defence", 8, 2);
addCharacteristics(characteristicsPanel, "Agility", 9, 3);
addCharacteristics(characteristicsPanel, "Luck", 2, 4);
JScrollPane imgListPane = new JScrollPane(charImgList);
imgListPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
charImgList.setVisibleRowCount(CHAR_IMG_VISIBLE_ROWS);
charImgList.setPrototypeCellValue(createProtoType());
JPanel rightPanel = new JPanel(new BorderLayout(EB_GAP, EB_GAP));
rightPanel.add(topBtnPanel, BorderLayout.PAGE_START);
rightPanel.add(imgListPane, BorderLayout.CENTER);
rightPanel.add(characteristicsPanel, BorderLayout.LINE_END);
JPanel optionsPanel = new JPanel(new GridLayout(1, 0));
for (int i = 0; i < NUMBER_OF_OPTIONS; i++) {
String text = "Option " + (i + 1);
optionsPanel.add(new JCheckBox(text));
}
currentTextArea.setWrapStyleWord(true);
currentTextArea.setLineWrap(true);
currentTextArea.setFocusable(false);
JScrollPane taScrollPane = new JScrollPane(currentTextArea);
taScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.add(taScrollPane, BorderLayout.CENTER);
centerPanel.add(rightPanel, BorderLayout.LINE_END);
centerPanel.add(optionsPanel, BorderLayout.PAGE_END);
JPanel commandsPanel = new JPanel();
commandsPanel.setLayout(new BoxLayout(commandsPanel, BoxLayout.LINE_AXIS));
commandsPanel.add(commandsField);
commandsPanel.add(Box.createHorizontalStrut(EB_GAP));
commandsPanel.add(new JButton(enterAction));
commandsPanel.add(Box.createHorizontalStrut(EB_GAP));
commandsPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
commandsField.setAction(enterAction); // use same action for button and
// text field
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
setLayout(new BorderLayout(EB_GAP, EB_GAP));
add(centerPanel, BorderLayout.CENTER);
add(commandsPanel, BorderLayout.PAGE_END);
}
private void addCharacteristics(JPanel cPanel, String text, int value, int row) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = row;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
cPanel.add(new JLabel(text), gbc);
gbc.insets.left = 20;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 1;
cPanel.add(new JLabel(String.valueOf(value)), gbc);
}
private Icon createProtoType() {
int w = USER_IMG_CHAR_IMG_WIDTH;
int h = w;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Icon icon = new ImageIcon(img);
return icon;
}
private class EnterAction extends AbstractAction {
public EnterAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = commandsField.getText();
currentTextArea.append(text + "\n");
commandsField.selectAll();
}
}
private class ExitAction extends AbstractAction {
public ExitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Component source = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(source);
win.dispose();
}
}
private static void createAndShowGUI() {
TomGuiPanel mainPanel = new TomGuiPanel();
JFrame frame = new JFrame("Tom's GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
将创建这个可实现的 GUI:
请注意,GUI 的制作很粗糙,除了输入和退出按钮之外没有任何功能。
关于JPanel 的 Java awt 边框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33452636/
AWT-EventQueue 线程和 AWT-Shutdown 线程没有在我们的应用程序中关闭。有没有一种调试技术可以找出它们不存在的原因?有什么特别的东西要寻找吗? 最佳答案 如果你的意思是关闭所有
java.awt.* 和 java.awt.event.* 有什么区别? 最佳答案 这只是两个不同的包。 当您说import java.awt.*时,它仅导入那些完全属于java.awt包的类,而不是
我正在将 aadhar 集成到 liferay 中。我尝试了这个链接 https://developer.uidai.gov.in/site/book/export/html/18 所以我想将其集成到
我想知道如何确定 Java.awt.Rectangle 是否包含特定 Java.awt.Color 的像素。我一直在到处寻找,但找不到任何关于此的信息,甚至找不到任何可能的信息。 所以我想知道如何确定
我试图在组件和图像之间切换面板的内容,它适用于组件: imgpanel.removeAll(); Component comp; if ((comp = player.getVisualCompone
我在使用 JAVA 编码时遇到一些错误,我一直在尝试解决这个问题,也试图找到其他有同样问题的人并修复它,但没有任何效果... 嗯..这是代码 package ca.vanzeben.game;
我想我可以尝试一下 JAXB 来处理存储和恢复设置。但即使是“最简单”的例子我也遇到了麻烦: import java.awt.Point; public class Config { public
这个问题已经有答案了: Import package.* vs import package.SpecificType [duplicate] (10 个回答) 已关闭 7 年前。 在我现在正在进行的
private static byte[] get_byte_data(BufferedImage image) { //WritableRaster raster = image.get
是否有可能获得标准 AWT Cursor以位图图像的形式(例如 BufferedImage )或任何可在 Graphics2D 上绘制的图像?例如,文本光标 new Cursor(Cursor.TEX
我的代码中有三个点,我想填充它们之间的区域,或者换句话说,在 3 个点之间绘制并填充一个三角形。 我想过简单地用 for 循环绘制线条(从 x1 到 x2),但我认为这不会有效,是否有其他更有效的方法
我正在制作一个小脚本,我使用鼠标键来节省我的工作时间。我可以正确、良好地使用鼠标键。但是,当使用 java.awt.Robot 和 java.awt.event.KeyEvent 时,鼠标键基本上被忽
我正在尝试在 scala 中使用 java awt 来制作一个简单的桌面应用程序。我已经在它上面工作了几天,没有任何问题,直到我有 2 天没有碰它,当我回来时,我得到一个 java.lang.NoCl
我在 VisualVM 和线程 View 中监视一个 JavaFX 程序,不断有 AWT-EventQueue-0 和 AWT-Shutdown 线程被创建和销毁。这是正常行为吗?这是什么原因? 最佳
我需要将 java.awt.geom.Area 或 java.awt.Shape 转换为 java.awt.Polygon。我所知道的是:isSingular = true、isPolygonal =
我正在重新使用 Java 并审查我的一些旧代码,并且我看到了很多我已经完成的地方 import javax.swing.*; import java.awt.*; 或者实际上从 swing/awt 包
晚上, 我在玩一个小的 swing 应用程序,我添加了一个按钮来响应按下。因此我需要实现 ActionListener。我已经添加了这一行: import java.awt.*; 但它告诉我找不到“A
我有这个 java 代码: Editor() { javax.swing.SwingUtilities.invokeLater(new Runnable() { pub
请帮我解决这个问题 sun.awt.image.ToolkitImage 无法转换为 java.awt.image.BufferedImage if (shape.hasImage())
嗨 Stackoverflow 的 friend 们 我最近将 Jenkins 服务器配置到 Apache Tomcat 7.0.42我制作的程序是将 jenkins.war 文件部署到 tomcat
我是一名优秀的程序员,十分优秀!