An *_*ity 1 java jpanel jframe
我有一个问题,当我在 JFrame 中包含 JPanel 时,它会覆盖整个屏幕。我的代码读取
import java.awt.*;
import javax.swing.*;
public class TestFrameExample extends JPanel {
static String TheQuestion;
static String QN1 = "Question 1: ";
static String Q1 = "What is Lead's chemical symbol?";
static String brk = "___________________";
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(135, 206, 250));
g.setFont(new Font("Tahoma", Font.BOLD, 48));
g.setColor(Color.WHITE);
g.drawString(QN1, 80, 100);
g.setFont(new Font("Tahoma", Font.BOLD, 48));
g.setColor(Color.WHITE);
g.drawString(brk, 60, 130);
g.setFont(new Font("Tahoma", Font.PLAIN, 36));
g.setColor(Color.WHITE);
g.drawString(Q1, 80, 200);
}
public static void main(String[] args) {
TestFrameExample graphics = new TestFrameExample();
JFrame ThisFrame = new JFrame();
TheQuestion = QN1;
JTextField txt = new JTextField(10);
JPanel Panel = new JPanel();
ThisFrame.setTitle("Question 1");
ThisFrame.setSize(720, 480);
ThisFrame.setLocationRelativeTo(null);
ThisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ThisFrame.setVisible(true);
ThisFrame.add(graphics);
Panel.setBackground(new Color(135, 206, 250));
Panel.setSize(null);
Panel.setLocation(null);
Panel.add(txt);
ThisFrame.add(Panel);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,当我改变
Panel.setLocation(null)
到
Panel.setLocation(80, 250)
它覆盖了整个屏幕。
有人可以帮我把它放在屏幕上的正确位置吗?
更新
我按照我在评论中所说的进行了修改,代码阅读
import java.awt.*;
import javax.swing.*;
public class TestFrameExample extends JPanel {
static String TheQuestion;
static String QN1 = "Question 1: ";
static String Q1 = "What is Lead's chemical symbol?";
static String brk = "___________________";
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(135, 206, 250));
g.setFont(new Font("Tahoma", Font.BOLD, 48));
g.setColor(Color.WHITE);
g.drawString(QN1, 80, 100);
g.setFont(new Font("Tahoma", Font.BOLD, 48));
g.setColor(Color.WHITE);
g.drawString(brk, 60, 130);
g.setFont(new Font("Tahoma", Font.PLAIN, 36));
g.setColor(Color.WHITE);
g.drawString(Q1, 80, 200);
}
public static void main(String[] args) {
TestFrameExample graphics = new TestFrameExample();
JFrame ThisFrame = new JFrame();
TheQuestion = QN1;
JTextField txt = new JTextField(20);
JPanel panel = new JPanel();
ThisFrame.setTitle("Question 1");
ThisFrame.setSize(720, 480);
ThisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ThisFrame.add(graphics);
panel.setBackground(new Color(135, 206, 250));
panel.add(txt);
ThisFrame.add(panel);
ThisFrame.add(panel, BorderLayout.PAGE_END);
ThisFrame.setLocationRelativeTo(null);
ThisFrame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
它将输入 txt 放在屏幕底部,但屏幕的其余部分保持灰色。
但我根本不明白你发布的第二组代码;这远远超出了我的技能范围(过去 6 周我只学习了 Java)。我在这个阶段所寻找的只是让面板不会使屏幕的其余部分空白。
这是否可以仅通过修改当前的代码集,或者我必须完全重写代码?
JFrame 的 contentPane 默认使用 BorderLayout,你可以利用它来发挥你的优势:
BorderLayout.CENTER到您的 JFrameBorderLayout.PAGE_END位置。这会将组件添加到 GUI 的底部。setLocation(...)您的组件,因为这会吸引使用null布局,这种布局会导致难以调试和升级的僵化 GUI。有关更多详细信息,请阅读教程中的 BorderLayout。
例如,
public static void main(String[] args) {
TestFrameExample graphics = new TestFrameExample();
JFrame thisFrame = new JFrame();
TheQuestion = QN1;
JTextField txt = new JTextField(10);
JPanel panel = new JPanel();
thisFrame.setTitle("Question 1");
thisFrame.setSize(720, 480); // better to not do this, but to pack instead
// thisFrame.setLocationRelativeTo(null);
thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// thisFrame.setVisible(true); // *** not yet ***
thisFrame.add(graphics, BorderLayout.CENTER);
panel.setBackground(new Color(135, 206, 250));
// panel.setSize(null); // *** don't do this ***
// panel.setLocation(null); // *** don't do this ***
panel.add(txt);
thisFrame.add(panel, BorderLayout.PAGE_END);
thisFrame.setLocationRelativeTo(null);
thisFrame.setVisible(true); // *** HERE ***
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果可能,我会避免直接绘制,而是使用组件和布局管理器。这应该可以更容易地显示不同类型的问题。例如,运行以下程序并通过<enter>反复按键,滚动问题以了解我的意思:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class TestQuestion2 extends JPanel {
private static final Color BACKGROUND = new Color(135, 206, 250);
private static final Color LABEL_FOREGROUND = Color.white;
private static final int EB_GAP = 60;
private static final int PREF_W = 720;
private static final int PREF_H = 480;
private static final Font TITLE_FONT = new Font("Tahoma", Font.BOLD, 48);
private static final Font Q_FONT = TITLE_FONT.deriveFont(Font.PLAIN, 36f);
private JLabel questionTitleLabel = new JLabel();
private JTextArea questionArea = new JTextArea(4, 10);
private JTextField answerField = new JTextField(20);
private Question question;
private List<Question> questionList;
private int questionListIndex = 0;
public TestQuestion2(List<Question> questionList) {
this.questionList = questionList;
questionTitleLabel.setFont(TITLE_FONT);
questionTitleLabel.setForeground(LABEL_FOREGROUND);
JSeparator separator = new JSeparator();
separator.setForeground(LABEL_FOREGROUND);
questionArea.setFont(Q_FONT);
questionArea.setForeground(LABEL_FOREGROUND);
questionArea.setWrapStyleWord(true);
questionArea.setLineWrap(true);
questionArea.setBorder(null);
questionArea.setOpaque(false);
questionArea.setFocusable(false);
JScrollPane scrollPane = new JScrollPane(questionArea);
scrollPane.setBorder(null);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
JPanel answerPanel = new JPanel();
answerPanel.add(answerField);
answerPanel.setOpaque(false);
setBackground(BACKGROUND);
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(questionTitleLabel);
add(Box.createHorizontalStrut(10));
add(separator);
add(Box.createHorizontalStrut(5));
add(scrollPane);
add(Box.createHorizontalGlue());
add(answerPanel);
setQuestion(questionList.get(questionListIndex));
answerField.addActionListener(new AnswerListener());
}
public void setQuestion(Question question) {
this.question = question;
questionTitleLabel.setText("Question " + question.getNumber() + ":");
questionArea.setText(question.getQuestion());
}
@Override
public Dimension getPreferredSize() {
Dimension superSz = super.getPreferredSize();
if (isPreferredSizeSet()) {
return superSz;
}
int prefW = Math.max(superSz.width, PREF_W);
int prefH = Math.max(superSz.height, PREF_H);
return new Dimension(prefW, prefH);
}
private class AnswerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
questionListIndex++;
questionListIndex %= questionList.size();
setQuestion(questionList.get(questionListIndex));
}
}
private static void createAndShowGui() {
List<Question> questionList = new ArrayList<>();
questionList.add(new Question(1, "What is Lead's chemical symbol?"));
questionList.add(new Question(2, "Who is buried in Grant's tomb?"));
questionList.add(new Question(3, "What ..... is your quest?"));
questionList.add(new Question(4, "What ..... is your favorite color?"));
questionList.add(new Question(5, "What is the capital of Assyria?"));
questionList.add(new Question(6, "What is the airspeed velocity of the unladen laden swallow?"));
questionList.add(new Question(7, "This will be a very long question, one that shows the display "
+ "of multiple lines, and a JTextArea that looks like a JLabel. "
+ "What do you think of it?"));
TestQuestion2 testQuestion2Panel = new TestQuestion2(questionList);
JFrame frame = new JFrame("Question");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(testQuestion2Panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Question {
private int number;
private String question;
private List<String> possibleAnswers;
public Question(int number, String question) {
this.number = number;
this.question = question;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1267 次 |
| 最近记录: |