- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我有一些关于使用拆分 Pane 将我的框架分成两个区域的技巧,但我无法让它显示一些有用的东西。代码如下所示:
public class Whiteboard extends JPanel {
int width = 600;
int sidePanelWidth = 200;
int lineHeight = 120;
int numberOfLines = 5;
JFrame frame = null;
Glyph glyph = null;
//java.awt.Rectangle bounds = new java.awt.Rectangle();
Bounds bounds = null;
JSplitPane splitPane = null;
JPanel tools = null;
public Whiteboard() {
frame = new JFrame();
frame.setSize(width + sidePanelWidth, getFullHeight());
FlowLayout simpleLayout = new FlowLayout();
frame.setLayout(simpleLayout);
tools = new JPanel();
tools.setSize(new Dimension(sidePanelWidth, getFullHeight()));
this.setSize(width, getFullHeight());
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this, tools);
splitPane.setPreferredSize(new Dimension(width + sidePanelWidth, getFullHeight()));
splitPane.setOneTouchExpandable(false);
splitPane.setDividerLocation(150);
frame.add(splitPane);
this.setBackground(Color.white);
java.awt.Rectangle rectBounds = this.getBounds();
bounds = new Bounds((int)rectBounds.getX(), (int)rectBounds.getY(), (int)(rectBounds.getX() + rectBounds.getWidth()), (int)(rectBounds.getY() + rectBounds.getHeight()));
}
public int getFullHeight() {
return lineHeight * numberOfLines;
}
我现在把代码改成这样:
public static void main(String[] args) {
int sidePanelWidth = 200;
JFrame frame = new JFrame();
Whiteboard whiteboard = new Whiteboard();
JPanel sidePanel = new JPanel();
sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight()));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.add(whiteboard, JSplitPane.LEFT);
splitPane.add(sidePanel, JSplitPane.RIGHT);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(splitPane);
frame.pack();
frame.setVisible(true);
whiteboard.repaint();
}
以及这个的构造函数:
public Whiteboard() {
this.setPreferredSize(new Dimension(width, getFullHeight()));
this.setBackground(Color.red);
现在不知道是哪里出了问题,可能是没有调用paintComponent方法吧。我尝试通过调用 repaint() 来强制它,但它没有帮助,它只是不调用这个组件
编辑:好吧,现在看来它毕竟是在调用 paintComponent 方法,但我仍然得到这样的屏幕:
如您所见,它看起来不太好。那么我目前主要方法的代码:
public static void main(String[] args) {
int sidePanelWidth = 200;
JFrame frame = new JFrame();
Whiteboard whiteboard = new Whiteboard();
JPanel sidePanel = new JPanel();
sidePanel.setPreferredSize(new Dimension(sidePanelWidth, whiteboard.getFullHeight()));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.add(whiteboard, JSplitPane.LEFT);
splitPane.add(sidePanel, JSplitPane.RIGHT);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(splitPane);
frame.pack();
frame.setVisible(true);
whiteboard.repaint();
}
知道如何更改它来解决问题吗?我需要发布其他方法吗?
最佳答案
从 JPanel
的构造函数中创建一个 JFrame
真的不应该这样做。
这是我创建的示例:
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
public class JavaApplication100 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JavaApplication100().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
private void initComponents(Container contentPane) {
JPanel mainPanel = new JPanel();
//create our 2 seperate panels (could be custom ones)
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
//add labels for viewing
leftPanel.add(new JLabel("LEFT"));
rightPanel.add(new JLabel("RIGHT"));
//just so you can see em or they would be small
leftPanel.setPreferredSize(new Dimension(300, 300));
rightPanel.setPreferredSize(new Dimension(300, 300));
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
//add panels to split pane
jsp.add(rightPanel, JSplitPane.RIGHT);
jsp.add(leftPanel, JSplitPane.LEFT);
mainPanel.add(jsp);//add splitpane to mainpanel
contentPane.add(mainPanel);
}
}
编辑/更新:
根据您的评论,如果您想在扩展 JPanel
的 WhiteBoard
中为背景覆盖 paintComponent(Graphics g)
着色,如下所示:
public class WhiteBoard extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
关于java - 与 JSplitPane 斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12482786/
我想在 64 位数字中设置各个位,并认为 uint64_t是这份工作的理想选择。我遇到了一些奇怪的问题,所以我做了一些实验: 我写了下面的测试程序: #include #include #incl
所以我知道 mapply 将按元素顺序遍历一个数组(或一系列数组)——我想知道是否有办法阻止它对某些参数执行此操作……。让我进一步解释 我有一组数据点,带有 X 和 Y 坐标……X1 X2 ……Xn
我是 android 编程的新手,我正在尝试创建一个好友列表类。 首先,我创建了一个从数据库加载对象的数组: friendArray = new Friend[NumberOfFriendsInDat
我目前正在开发一个应用程序,该应用程序定期(每 30 秒)使用 json 格式从两个指定的足球队获取英国境内的两条推文。在 json 文件中,我可以访问当前文本的每条推文的位置(曼彻斯特、布里斯托尔等
我正在尝试编写一些 javascript 来询问用户,当他们离开页面时,是否要填写调查(无论这可能多么烦人!)。我认为我的解决方案是通过此站点上的答案找到的。这是我目前拥有的代码,但似乎无法正常工作:
好吧,我有一些关于使用拆分 Pane 将我的框架分成两个区域的技巧,但我无法让它显示一些有用的东西。代码如下所示: public class Whiteboard extends JPanel { i
我在 mod_rewrite htaccess 上苦苦挣扎了至少几天,但仍然无法解决这个问题。 我想在我的网站上强制使用 HTTPS SSL,但仅限于网络外部。 我有这样的东西: RewriteCon
我有一个非常简单的 XML 文件,需要用 xmlstarlet 解析(我在 Windows 下): IDENTIFYER 2013-01-25T11:15:
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 2年前关闭。 Improve t
我真的很努力地处理类、扩展和实现。 在 android 中,我想创建一个首选项屏幕并检查用户输入。 因此,我可以使用名为preferences.class的文件,并使用以下类执行我需要的所有操作:
我正在尝试运行 hello world boost python example乍一看一切都很好。我得到以下输出 >bjam ...patience... ...patience... ...foun
抱歉打扰大家了,这让我很痛苦。这是代码: char buf[500]; sprintf(buf,"D:\\Important\\Calibration\\Results\\model_%i.xml",
我最近了解了在 C++ 中使用反向迭代器的正确方法(特别是当您需要删除一个时)。 (参见 this question 和 this one。) 你应该这样做: typedef std::vector
我在使用 flex-layout 时遇到了一些问题。 我的代码通常如下所示: 我希望第二个 flex d
我遇到一个问题,一个简单的函数在到达 std::cin 请求时似乎终止了。编译器不会抛出任何警告或错误,也不会发生运行时错误,程序只会返回到主菜单。 片段: #include #include u
好的,希望这对您来说应该很容易。我是 git、BitBucket 的新手,也是版本控制的新手,我会遗漏一些对您来说很明显的东西。 所以这就是我所做的:我在我的机器上安装了 git 并设置了我的全局变量
我正在处理一个需要高度自动化的 Oracle 过程,因此我正在使用动态 SQL。我刚开始使用动态 SQL。 我正在使用 varchar2 变量来构建需要执行的 SQL。我使用了 dbms_output
我正在学习 python 和 Kivy,并且我真的很难理解如何从 Kivy GUI 调用函数和继续函数。 这是我的.py: import csv import os import easygui im
我试图在 WebView 中显示一个用 LaTex 编写的数学公式,但我在字符“\”上挣扎:我不能写“\”,因为这是错误的,但如果我使用“\\"应用无法呈现公式。 url += "This is a
当我只有一项时,我在使用 ng-repeat 时遇到了一些问题。 这是我的 Controller 中的部分代码: Data.get('company').then(function(data){
我是一名优秀的程序员,十分优秀!