- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在本周六到期的学校项目中,我遇到了我遇到过的最烦人的错误。我似乎无法在万维网上的任何地方找到解决方案。就连我的老 friend 谷歌似乎也让我失望了。那么,问题来了:
我正在使用 JTabbedPane。在这个 JTabbedPane 中,我将展示不同的 JPanes,每个 JPanes 都显示它们的信息。在其中一个 JPanes 中,我正在绘制条形图。
单独测试条形图时,一切正常。当我在 JTabbedPane 的右侧选项卡中加载我的信息时,它也会执行此操作。但是,一旦我切换回此选项卡,条形图就会在绘画过程中向上移动,看起来全都变形了(见图)。
我知道我总是在正确的坐标中绘图,所以一定是我的 JPanel 取代了 JTabbedPane 的选项卡。你们知道是什么导致了这种奇怪的行为吗?
对:应该怎样。左图:切换回此选项卡后的样子。
我的 BarGraphPanel 类是一个真正的困惑,这里是代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author Scuba Kay
*/
public class BarGraphPanel extends JPanel implements ManagementPanel {
private ArrayList<String[]> info;
private String infoName;
private double maxValue;
private int size;
private int barWidth;
private double left;
private double right;
private double width;
private double height;
private double bottom;
private double top;
private int barSpacing;
private double barPart;
private double graphHeight;
private double graphWidth;
private int cap;
public BarGraphPanel(){
super();
width = 600;
height = 480;
setSize((int) width, (int) height);
// The maximum number of results to show
cap = 30;
this.infoName = "Management information";
this.add(new JLabel("This query is not suited for Bar Graph view: please select another one."));
}
/**
* Sets both x and y, min and max and the info list
* @author Kay van Bree
* @param info
*/
@Override
public void setManagementInfo(ArrayList<String[]> info) {
reset();
this.info = info;
// Set important values
this.maxValue = getMaxValue(info);
this.size = info.size();
setSizes();
repaint();
}
/**
* Resets the panel, taken from TablePanel
* @author Joepe Kemperman
*/
public void reset() {
removeAll();
invalidate();
validate();
}
/**
* Set the sizes needed for calculating bar heights etc.
* @author Kay van Bree
*/
protected void setSizes(){
left = (width / 100) * 7;
right = width - left*3;
top = (height / 100) * 7;
bottom = height - top*3;
// The dimensions of the graph
graphHeight = bottom - top;
graphWidth = right - left;
// barWidth is... Just guess what it is.
barWidth = (int) ((double) (graphWidth / size) * 0.95);
// barSpacing is the space between the bars
barSpacing = (int) (graphWidth - (barWidth*size)) / size;
if(barSpacing == 0){
barSpacing++;
barWidth--;
}
// barPart is the height of a bar of value 1
barPart = graphHeight / maxValue;
}
/**
* Return the max value of the info arraylist
* @author Kay van Bree
* @param info
*/
private int getMaxValue(ArrayList<String[]> info){
int max = 0;
for(String[] row: info){
if(Integer.valueOf(row[1]) > max){
max = Integer.valueOf(row[1]);
}
}
return max;
}
/**
* Draw the damn thing!
* @author Kay van Bree
* @param g
*/
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(info != null){
g.setColor(Color.BLACK);
drawTitle(g);
drawBlocks(g);
if(size <= cap){
drawBars(g);
} else {
drawError(g);
}
drawAxes(g);
} else {
this.setInformationNotSuitable();
}
}
/**
* Set an error displaying to many results
* @author Kay van Bree
* @param g
*/
private void drawError(Graphics g){
g.setColor(Color.BLACK);
g.drawString("Too many results to show!", (int)(right-left*2)/2, (int)(bottom-top*2)/2);
}
/**
* Draw the names under the bars
* @author Kay van Bree
* @param g
*/
private void drawName(Graphics g, String name, int x){
Graphics2D g2d = (Graphics2D)g;
// clockwise 90 degrees
AffineTransform at = new AffineTransform();
// thanks to M.C. Henle for the bug fix!
at.setToRotation(Math.PI/2.0, x + (barWidth/4), bottom + 3);
g2d.setTransform(at);
g2d.setColor(Color.BLACK);
g2d.drawString(name, (int) (x + (barWidth/4)), (int) bottom + 3);
g2d.setTransform(new AffineTransform());
}
/**
* Draw the lines behind the bars
* @author Kay van Bree
* @param g
*/
private void drawBlocks(Graphics g){
g.setColor(Color.lightGray);
// Ten parts
double part = maxValue / 10;
double total = maxValue;
// Draw the numbers
for(int i=0; i<10; i++){
double y = (((bottom-top)/10)*i)+top;
g.drawLine(((int) left)-5, (int) y, ((int) right), (int) y);
}
g.drawLine((int) right, (int) top, (int) right, (int) bottom);
}
/**
* Draw the title of the Graph
* @author Kay van Bree
* @param g
*/
private void drawTitle(Graphics g){
int tLeft = 100;
g.drawString(infoName, tLeft, (int) top / 2);
}
/**
* Draw the axes of this Bar Graph
* @author Kay van Bree
* @param g
*/
private void drawAxes(Graphics g){
g.setColor(Color.BLACK);
// draw lines from (x, y) to (x, y)
g.drawLine((int) left, (int) top, (int) left, (int) bottom);
g.drawLine((int) left, (int) bottom, (int) right, (int) bottom);
// Ten parts
double part = maxValue / 10;
double total = maxValue;
// Draw the numbers
for(int i=0; i<10; i++){
double y = (((bottom-top)/10)*i)+top;
String number = round(total);
FontMetrics fontMetrics = g.getFontMetrics();
g.drawString(number, (int) (left * 0.80) - fontMetrics.stringWidth(number), ((int) y)+5);
total = (total - part);
g.drawLine(((int) left)-5 , (int) y, ((int) left), (int) y);
}
}
/**
* Rounds the number to 1 decimal
* @author Kay van Bree
* @param number
* @return
*/
private String round(double number){
DecimalFormat df = new DecimalFormat("#.#");
Double dubbel = new Double(number);
return df.format(dubbel);
}
/**
* Round a number, make it int
* @author Kay van Bree
* @param number
* @return
*/
private int roundToInt(double number){
DecimalFormat df = new DecimalFormat("#");
Double dubbel = new Double(number);
return Integer.valueOf(df.format(dubbel));
}
/**
* We need info right?
* Then draw the damn bars already!
* @author Kay van Bree
* @param g
*/
private void drawBars(Graphics g){
double currentX = left + barSpacing + 1;
for(String[] row: info){
double value = Integer.valueOf(row[1]);
double barHeight = (value * barPart);
System.out.println("Value: " + value + " height: " + barHeight + " top: " + top);
double barStart = bottom - barHeight;
System.out.println("Barstart: " + barStart);
barHeight = ((int) bottom - (int) barStart);
g.setColor(Color.BLUE);
g.fillRect((int) currentX, (int) barStart, (int) barWidth, roundToInt(barHeight));
drawName(g, row[0], (int) currentX);
currentX = (currentX + barSpacing + barWidth);
}
}
public void setInformationNotSuitable() {
// JOptionPane.showMessageDialog(this, "This query is not suited for Bar Graph view. Please select another", "INSANE WARNING!", JOptionPane.WARNING_MESSAGE);
}
}
一些有用的信息:
setManagementInformation 由 JTabbedPane 调用一次。在这里它加载所有内容(还有其他选项卡)
编辑好的,至强的回答几乎解决了我的问题。每次我切换回此选项卡时,图表的条形现在都完美对齐。但是,切换回来时,栏下方的名称仍在移动。这是我更新的代码:
/**
* Draw the names under the bars
* @author Kay van Bree
* @param g
*/
private void drawName(Graphics g, String name, int x){
Graphics2D g2d = (Graphics2D) g.create();
// Clockwise 90 degrees
AffineTransform at = new AffineTransform();
// Rotate the text
at.setToRotation(Math.PI/2.0, x + (barWidth/4), bottom + 3);
AffineTransform prev = g2d.getTransform();
g2d.setTransform(at);
g2d.setColor(Color.BLACK);
g2d.drawString(name, (int) (x + (barWidth/4)), (int) bottom + 3);
g2d.dispose();
}
那么,这是否也与 AffineTransform 有关,有没有人能解决这个问题?
最佳答案
好的。这很棘手:
问题是 drawName
方法 - 您正在转换 graphics
实例而不是恢复它 - 您只是重置转换矩阵(所有值都为 0)。
你应该恢复它:
private void drawName(Graphics g, String name, int x) {
Graphics2D g2d = (Graphics2D) g;
// clockwise 90 degrees
AffineTransform at = new AffineTransform();
// thanks to M.C. Henle for the bug fix!
at.setToRotation(Math.PI / 2.0, x + (barWidth / 4), bottom + 3);
AffineTransform prev = g2d.getTransform();
g2d.setTransform(at);
g2d.setColor(Color.BLACK);
g2d.drawString(name, (int) (x + (barWidth / 4)), (int) bottom + 3);
g2d.setTransform(prev);
}
或者你甚至可以使用:
Graphics2D g2d = (Graphics2D) g.create();
...
// free to manipulate Graphics2D
,,,
d2d.dispose();
我建议使用 JFreeChart而不是编写自己的图表库。
关于java - 切换回来后 JTabbedPane 的内容移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10834834/
我编写了一个小程序,用于创建带有选项卡式 Pane 的简单 GUI。目前它有两个选项卡。在一个选项卡中我创建了一个按钮,在另一个选项卡中我创建了两个按钮(基于第三类中的字符串)。我无法做的是在第二个选
我想在单击负责使用 Action Listener 计算的按钮后将选定的 JTabbedPane“计算”更改为 TabbedPane“结果”。 最佳答案 class ButtonHandler imp
收到此错误: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Project.reportpane.a
我遇到的问题是,在 JTabbedPane 内部时,内部面板的背景颜色未设置(或未显示) 我有以下结构: JPanel BorderLayout: JTabbedPane (BorderLayo
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
在我的 JTabbedPane 中,我以两种不同的方式删除标签: tabbedPane.remove(index) 和 tabbedPane.removeAll() 两者在关闭选项卡方面都可以正常工作
我有一个使用 JTabbedPane 来显示多个不同选项卡的应用程序。这些选项卡之一有一个正在运行的线程来显示其内容。我已经实现了一个 ComponentListener 来在选项卡不可见时停止线程。
我想让我的选项卡标签共享 JTabbedPane 宽度。如果有一个标签,适合整个宽度,如果两个标签共享宽度,如果三个,每个 1/3,依此类推... 我什至不知道是否可以在不将组件放在那里并调整其大小的
我想知道如何从左侧开始设置 JTabbedPane 的选项卡位置。默认情况下,它们处于中间位置,因为我使用的是 Mac OS X。 如果您需要更多信息,请告诉我。 查看此图像以了解我使用默认 JTab
在这个 JTabbedPane 截图中 我正在使用 Netbeans 的 DnD,我一直在修改它只是为了在选项卡之间放置空格。到现在我还是得不到我想要的。 有办法吗? 最佳答案 我不确定其他方法,但我
我对java相当陌生,正在创建一个windowbuilder程序。我想知道在使用 Jtabbedpane 并在程序窗口中的选项卡之间切换时是否可以使用 Action 监听器从单独的类获取内容。例如,我
给定一个 contentPane 布局设置为 null 的 JFrame,我想添加两个选项卡,一个用于发布者,另一个用于订阅者,例如: public class PubSubGUI extends J
在 JTabbedPane 中,选项卡的顺序是从下到上: 我希望它是从上到下的,这意味着 tab0 位于顶行,tab60 位于底行。 代码: import javax.swing.JFrame; im
这类似于问题How to build a Google-chrome tabs and menubar interface in Java Swing? (我想完成同样的任务),但更重要的是:如何将组
我想将 JTabbedPane 放在中间,单击我想要更改的任何选项卡将反射(reflect)在选项卡 Pane 的上方和下方面板中。 我尝试过,但它只适用于下面的面板。 如何克服这个问题?请帮助我。
我创建了一个 Java 选项卡式 Pane 。在选项卡式 Pane 中,我创建了一个名为“视频”的选项卡。我希望每当我单击视频选项卡时,视频都会自动在选项卡面板本身上播放。我可以这样做吗?如果是,请告
我有一个很长的数据列表要在 jtabbedpanel 上显示,向下滚动数据时,顶部的选项卡式面板名称将不可见。我怎样才能使选项卡式面板选项卡在顶部可见,甚至在向下滚动? 任何人都可以帮助我解决这个问题
我进行了快速研究来解决这个问题,但到目前为止我没有发现任何与此相关的信息。我将一张图像放入一个 TabbedPane 对象中,但是当我尝试将此图像与 TabbedPane 内的标签中心对齐时,它“不起
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我是一名优秀的程序员,十分优秀!