- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个具有 JTabbedPane 的 GUI,并显示 RF 网络上 radio 之间的连接。我无法弄清楚如何使 tabbedPanes 动态创建。我尝试了很多事情但没有成功。这是我到目前为止所拥有的。
这是绘制类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class draw extends JPanel implements MouseListener{
private Color currentColor = Color.RED;
//Array to hold the grid dimensions used for drawing radio[]
private int[] gD = {40, 100, 160, 220, 280, 340, 400, 460};
//Flag for changing colors in the grid
private boolean colorChanged = false;
public void drawing() {
repaint();
}
public void paintComponent(final Graphics g) {
super.paintComponent(g);
final Graphics2D g2d = (Graphics2D)g;
//Smooth Graphics
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//Quality Color Rendering
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_QUALITY);
//Array to hold 40 radios
final Ellipse2D.Double radio[] = new Ellipse2D.Double[39];
//Array to hold lines that show radio connections
final Line2D connection[] = new Line2D[49];
/****************************************************************
This section of code draws the Ellipses that represent the
different radios.
*****************************************************************/
radio[0] = new Ellipse2D.Double(gD[2], gD[0], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[0]);
this.addMouseListener(this);
radio[1] = new Ellipse2D.Double(gD[1], gD[1], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[1]);
this.addMouseListener(this);
radio[2] = new Ellipse2D.Double(gD[0], gD[2], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[2]);
this.addMouseListener(this);
radio[3] = new Ellipse2D.Double(gD[1], gD[3], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[3]);
this.addMouseListener(this);
radio[4] = new Ellipse2D.Double(gD[2], gD[4], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[4]);
this.addMouseListener(this);
radio[5] = new Ellipse2D.Double(gD[5], gD[0], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[5]);
this.addMouseListener(this);
radio[6] = new Ellipse2D.Double(gD[6], gD[1], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[6]);
this.addMouseListener(this);
radio[7] = new Ellipse2D.Double(gD[7], gD[2], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[7]);
this.addMouseListener(this);
radio[8] = new Ellipse2D.Double(gD[6], gD[3], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[8]);
this.addMouseListener(this);
radio[9] = new Ellipse2D.Double(gD[5], gD[4], gD[0], gD[0]);
g2d.setPaint(currentColor);
g2d.fill(radio[9]);
this.addMouseListener(this);
/****************************************************************
This section of code draws the lines that represent the
connections to the different radios.
*****************************************************************/
connection[0] = new Line2D.Double(radio[0].getCenterX(), radio[0].getCenterY(), radio[9].getCenterX(), radio[9].getCenterY());
g2d.draw(connection[0]);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
/**************************************************************************
This block of statements changes the radio buttons to green when clicked
and sets the colorChanged flag to true.
*************************************************************************/
if (!colorChanged && radio[0].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[2], gD[0], gD[0], gD[0]);
} else if (!colorChanged && radio[1].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[1], gD[1], gD[0], gD[0]);
} else if (!colorChanged && radio[2].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[0], gD[2], gD[0], gD[0]);
} else if (!colorChanged && radio[3].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[1], gD[3], gD[0], gD[0]);
} else if (!colorChanged && radio[4].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[2], gD[4], gD[0], gD[0]);
} else if (!colorChanged && radio[5].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[5], gD[0], gD[0], gD[0]);
} else if (!colorChanged && radio[6].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[6], gD[1], gD[0], gD[0]);
} else if (!colorChanged && radio[7].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[7], gD[2], gD[0], gD[0]);
} else if (!colorChanged && radio[8].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[6], gD[3], gD[0], gD[0]);
} else if (!colorChanged && radio[9].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.GREEN;
colorChanged = true;
repaint(gD[5], gD[4], gD[0], gD[0]);
/***************************************************************************
This block of statements changes the radio buttons to red when clicked
and sets the colorChanged flag to false
***************************************************************************/
} else if (colorChanged && radio[0].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[0], gD[0], gD[0], gD[0]);
} else if (colorChanged && radio[1].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[0], gD[1], gD[0], gD[0]);
} else if (colorChanged && radio[2].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[0], gD[2], gD[0], gD[0]);
} else if (colorChanged && radio[3].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[0], gD[3], gD[0], gD[0]);
} else if (colorChanged && radio[4].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[0], gD[4], gD[0], gD[0]);
} else if (colorChanged && radio[5].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[7], gD[0], gD[0], gD[0]);
} else if (colorChanged && radio[6].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[7], gD[1], gD[0], gD[0]);
} else if (colorChanged && radio[7].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[7], gD[2], gD[0], gD[0]);
} else if (colorChanged && radio[8].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[7], gD[3], gD[0], gD[0]);
} else if (colorChanged && radio[9].contains(e.getX(), e.getY())) {
e.getSource();
currentColor = Color.RED;
colorChanged = false;
repaint(gD[7], gD[4], gD[0], gD[0]);
}
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
}
这是 frontPage 类
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class FrontPage extends JFrame{
private JPanel rootPanel;
private JButton buttonPushConfiguration;
private JTabbedPane tabbedPane;
public FrontPage() {
this.setBounds(new Rectangle(0, 0, 600, 600));
setContentPane(rootPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
draw object = new draw();
//Creates the first tab
tabbedPane.addTab("Switch 1", object);
object.drawing();
buttonPushConfiguration.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(FrontPage.this, "Configuration Pushed to Panel");
}
});
}
}
这是主类
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//Set the theme to the current system theme.
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch(UnsupportedLookAndFeelException e){
// handle exception
}
catch(ClassNotFoundException e){
// handle exception
}
catch(InstantiationException e){
// handle exception
}
catch(IllegalAccessException e){
}
//Instantiates FrontPage
new FrontPage();
}
}
最佳答案
如果您的目标是在按钮的 ActionListener 内向 JTabbedPane 添加一个新的 JPanel,那么这正是您需要做的,例如:
@Override
public void actionPerformed(ActionEvent e) {
// int variable for the tab title
tabIndex++;
String title = "Switch " + tabIndex;
// I renamed your draw class to conform to Java standards
DrawPanel tabComponent = new DrawPanel();
tabbedPane.add(title, tabComponent);
}
如果您需要做更多事情,请告诉我们。我还必须重申我对您原始帖子的评论:
MouseListener 更改点颜色的示例:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class DrawPanelMain extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private List<Point> POINT_LIST = Arrays.asList(new Point[] {
new Point(160, 40),
new Point(100, 100),
new Point(40, 160),
new Point(100, 220),
new Point(160, 280),
new Point(340, 40),
new Point(400, 100),
new Point(460, 160),
new Point(400, 220),
new Point(340, 280)
});
private JTabbedPane tabbedPane = new JTabbedPane();
private int tabIndex = 0;
public DrawPanelMain() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new PushConfigAction("Push Config")));
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class PushConfigAction extends AbstractAction {
public PushConfigAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
tabIndex++;
String title = "Switch " + tabIndex;
DrawPanel2 tabComponent = new DrawPanel2(POINT_LIST);
tabbedPane.add(title, tabComponent);
}
}
private static void createAndShowGui() {
DrawPanelMain mainPanel = new DrawPanelMain();
JFrame frame = new JFrame("DrawPanelMain");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
@SuppressWarnings("serial")
class DrawPanel2 extends JPanel {
private static final int OVAL_WIDTH = 40;
private static final Color INACTIVE_COLOR = Color.RED;
private static final Color ACTIVE_COLOR = Color.green;
private List<Point> points;
private List<Ellipse2D> ellipses = new ArrayList<>();
private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();
public DrawPanel2(List<Point> points) {
this.points = points;
for (Point p : points) {
int x = p.x - OVAL_WIDTH / 2;
int y = p.y - OVAL_WIDTH / 2;
int w = OVAL_WIDTH;
int h = OVAL_WIDTH;
Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
ellipses.add(ellipse);
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
MyMouseAdapter mListener = new MyMouseAdapter();
addMouseListener(mListener);
addMouseMotionListener(mListener);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ellipse2D ellipse : ellipses) {
g2.setColor(ellipseColorMap.get(ellipse));
g2.fill(ellipse);
}
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
for (Ellipse2D ellipse : ellipses) {
if (ellipse.contains(e.getPoint())) {
Color c = ellipseColorMap.get(ellipse);
c = (c == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
ellipseColorMap.put(ellipse, c);
}
}
repaint();
}
}
}
关于java - 动态创建 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371918/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!