作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在设计一个与DFA相关的程序..所以我想将Q0或Q1之类的东西放在圆圈内,但我不知道如何..有人可以看看我的代码并告诉我如何做吗?如果可以直接放置一个出现在圆圈内的名称,那就太好了,因为我认为很难为 JLabel 设置位置..这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class midterm extends JFrame
{
JPanel mainpanel;
JPanel gamepanel;
JPanel controls;
ExitButtonListener end=new ExitButtonListener();
public midterm()
{
super("My DFA Design");
setSize(700,700);
setLocation(400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
panel();
this.getContentPane().add(mainpanel);
this.pack();
setVisible(true);
setSize(400,400);
}
public static void main(String[] args)
{
midterm frame=new midterm();
}
void panel()
{
mainpanel=new JPanel();
mainpanel.setLayout(new BorderLayout());
gamepanel=new JPanel();
gamepanel.setBorder(BorderFactory.createTitledBorder("Deterministic Finite Automata"));
gamepanel.setLayout(new GridLayout(2,3));
controls = new JPanel();
controls.setLayout(new BorderLayout());
controls.setBorder(BorderFactory.createTitledBorder("Control"));
JButton newGame = new JButton("Reset");
newGame.addActionListener(new NewButtonListener());
controls.add(newGame, BorderLayout.NORTH);
JButton exitGame = new JButton("Exit");
exitGame.addActionListener(end);
controls.add(exitGame, BorderLayout.SOUTH);
mainpanel.add(gamepanel, BorderLayout.CENTER);
mainpanel.add(controls, BorderLayout.EAST);
mainpanel.setVisible(true);
q1 c1=new q1();
q2 c2=new q2();
q3 c3=new q3();
gamepanel.add(c1);
gamepanel.add(c2);
gamepanel.add(c3);
}
class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
class NewButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
class q1 extends JPanel
{
final int radius=25;
public void paint(Graphics gr)
{
setBackground(Color.black);
gr.drawOval((100/2-radius),(100/2-radius), radius*2, radius*2);
}
}
class q2 extends JPanel
{
final int radius=25;
public void paint(Graphics gr)
{
setBackground(Color.black);
gr.drawOval((95/2-radius),(100/2-radius), radius*2, radius*2);
}
}
class q3 extends JPanel
{
final int radius=25;
public void paint(Graphics gr)
{
setBackground(Color.black);
gr.drawOval((250/2-radius),(50/2-radius), radius*2, radius*2);
}
}
}
最佳答案
可能,是,推荐,也许不......
public class TestLabel {
public static void main(String[] args) {
new TestLabel();
}
public TestLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
add(new CircleLable("1"));
add(new JLabel("What is the capital of SegWay?"));
}
}
public class CircleLable extends JLabel {
public CircleLable() {
init();
}
public CircleLable(String text) {
super(text);
init();
}
protected void init() {
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
setBorder(new EmptyBorder(2, 2, 2, 2));
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width = Math.max(size.width, size.height);
size.height = size.width;
return size;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Insets insets = getInsets();
int width = (getWidth() - (insets.left + insets.right));
int height = getHeight() - (insets.top + insets.bottom);
int radius = Math.max(width, height);
int x = insets.left + ((width - radius) / 2);
int y = insets.top + ((height - radius) / 2);
g2d.drawOval(x, y, radius, radius);
super.paintComponent(g2d);
g2d.dispose();
}
}
}
我对这个解决方案的问题是有太多可能出错的地方。添加图标、更改文本对齐方式等都可以通过它关闭。
就我个人而言,我会简单地使用自定义 JPanel
并自行渲染文本(确保组件首先是透明的),但这只是我的情况......
关于java - 如何在 JFrame 中的圆圈内添加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14670482/
我是一名优秀的程序员,十分优秀!