- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 drawToScreen
方法内调用 getGraphics()
时,我的 JPanel
预览返回 null。 Test 类也扩展了 JPanel
,因为它被保存在 TabbedPane
中。该类还实现了 Runnable、KeyListener 和 MouseListener
System.out.println
的日志
是
javax.swing.JPanel[,172,149,1280x720,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=null]
下面是现有代码的示例。运行它是行不通的,因为它使用外部方法来完成自身,但希望能找到答案。
public Test() {
setBackground(Color.DARK_GRAY);
setLayout(null);
preview = new JPanel();
preview.setBounds(172, 149, 1280, 720);
add(preview);
}
public void addNotify() {
preview.addNotify();
if(thread == null) {
thread = new Thread(this);
preview.addKeyListener(this);
preview.addMouseListener(this);
thread.start();
}
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_ARGB);
g = (Graphics2D) image.getGraphics();
running = true;
}
private long redraw() {
long t = System.currentTimeMillis();
if(onTab) {
if(!FileManager.isSleeping())
update();
if(!pause) {
draw();
drawToScreen();
}
}
return System.currentTimeMillis() - t;
}
public void run() {
init();
while(running) {
long durationMs = redraw();
try {
Thread.sleep(Math.max(0, FPS - durationMs));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void update () {
reupdateImages();
}
private void draw() {
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
for(int i = 0; i < layers.length; i++) {
if(layers[i] != null) {
g.drawImage(
layers[i].getImage(),
layers[i].getX(),
layers[i].getY(),
layers[i].getWidth(),
layers[i].getHeight(),
null
);
g.setColor(Color.red);
for(int j = 0; j < layers.length; j++) {
if(layers[j].isSelected())
g.drawRect(
layers[j].getX(),
layers[j].getY(),
layers[j].getWidth(),
layers[j].getHeight()
);
}
}
}
}
private void drawToScreen() {
System.out.println(preview);
System.out.println(preview.getGraphics());
Graphics g2 = preview.getGraphics();
g2.drawImage(image, 0, 0,
WIDTH, HEIGHT,
null);
panelImage = image;
g2.dispose();
}
至于代码的其余部分,根据 markspace 的要求,我添加了其余部分,只是为了在提供的示例之外揭穿。
@SuppressWarnings("serial")
public class ThumbnailEditor extends JPanel implements Runnable, KeyListener, MouseListener {
private JComboBox layerBox;
private int pos = 0;
private JFileChooser jfc;
// automated gui, I'm lazy and its dynamic
private int numberOfLayers = 24; // even numbers only guys and keep above 8
private JPanel preview;
private JSpinner localWidth;
private JSpinner localHeight;
private JSpinner posx;
private JSpinner posy;
private boolean ignore = false;
private JTextField width;
private JTextField height;
private JButton remove;
private JButton add;
private JButton select;
private JButton edit;
// drawing stoof
public int WIDTH = 1280;
public int HEIGHT = 720;
private Thread thread;
private boolean running;
private int FPS = 60;
private File location;
private BufferedImage panelImage;
private BufferedImage image;
private Graphics2D g;
private boolean pause = false;
private boolean onTab = false;
// when it gets too big, annoying issues start to happen with the text. This automatically fixes it
private static int[] overrideSizes = {
8,
9,
10,
11,
12,
14,
16,
18,
20,
22,
24,
26,
28,
36,
48,
72
};
// adjust this, low the more sensitive the changing of the font is.
private static int sensitivity = 4;
// layer stoof
// layer 0 --> at 0. layer 1 --> at 1
private static ThumbnailObject[] layers;
public static TextEditor[] te;
public ThumbnailEditor() {
setBackground(Color.DARK_GRAY);
setLayout(null);
preview = new JPanel();
preview.setBounds(172, 149, 1280, 720);
add(preview);
layerBox = new JComboBox();
layerBox.setBackground(Color.DARK_GRAY);
layerBox.setFont(new Font("Arial Black", Font.BOLD, 14));
layerBox.setBounds(10, 11, 100, 40);
add(layerBox);
add = new JButton("Add");
add.setBackground(Color.DARK_GRAY);
add.setFont(new Font("Arial Black", Font.BOLD, 14));
add.setBounds(120, 11, 100, 40);
add(add);
select = new JButton("Select");
select.setBackground(Color.DARK_GRAY);
select.setFont(new Font("Arial Black", Font.BOLD, 14));
select.setBounds(230, 11, 100, 40);
add(select);
edit = new JButton("Edit");
edit.setBackground(Color.DARK_GRAY);
edit.setFont(new Font("Arial Black", Font.BOLD, 14));
edit.setBounds(340, 11, 100, 40);
add(edit);
remove = new JButton("Remove");
remove.setBackground(Color.DARK_GRAY);
remove.setFont(new Font("Arial Black", Font.BOLD, 14));
remove.setBounds(450, 11, 100, 40);
add(remove);
JButton generate = new JButton("Generate Test Image");
generate.setBackground(Color.DARK_GRAY);
generate.setFont(new Font("Arial Black", Font.BOLD, 14));
generate.setBounds(1376, 11, 239, 40);
add(generate);
JButton deselect = new JButton("Deselect All Layers");
deselect.setBackground(Color.DARK_GRAY);
deselect.setFont(new Font("Arial Black", Font.BOLD, 14));
deselect.setBounds(120, 62, 210, 40);
add(deselect);
JLabel widthLabel = new JLabel("Width");
widthLabel.setHorizontalAlignment(SwingConstants.CENTER);
widthLabel.setForeground(Color.WHITE);
widthLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
widthLabel.setBounds(10, 149, 70, 40);
add(widthLabel);
JLabel heightLabel = new JLabel("Height");
heightLabel.setHorizontalAlignment(SwingConstants.CENTER);
heightLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
heightLabel.setForeground(Color.WHITE);
heightLabel.setBounds(90, 149, 70, 40);
add(heightLabel);
localWidth = new JSpinner();
localWidth.setFont(new Font("Arial Black", Font.BOLD, 14));
localWidth.setBounds(10, 184, 70, 20);
add(localWidth);
localHeight = new JSpinner();
localHeight.setFont(new Font("Arial Black", Font.BOLD, 14));
localHeight.setBounds(90, 184, 70, 20);
add(localHeight);
JLabel xSizeLabel = new JLabel("X");
xSizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
xSizeLabel.setForeground(Color.WHITE);
xSizeLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
xSizeLabel.setBounds(10, 215, 70, 40);
add(xSizeLabel);
JLabel ySizeLabel = new JLabel("Y");
ySizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
ySizeLabel.setForeground(Color.WHITE);
ySizeLabel.setFont(new Font("Arial Black", Font.BOLD, 14));
ySizeLabel.setBounds(90, 215, 70, 40);
add(ySizeLabel);
posx = new JSpinner();
posx.setFont(new Font("Arial Black", Font.BOLD, 14));
posx.setBounds(10, 248, 70, 20);
add(posx);
posy = new JSpinner();
posy.setFont(new Font("Arial Black", Font.BOLD, 14));
posy.setBounds(92, 248, 70, 20);
add(posy);
pause = true;
layers = new ThumbnailObject[numberOfLayers];
te = new TextEditor[numberOfLayers];
for(int i = 0; i < layers.length; i++) {
layers[i] = new ThumbnailObject();
}
for(int i = 0; i < numberOfLayers; i++) {
layerBox.addItem("Layer [" + i + "]");
}
remove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
pos = layerBox.getSelectedIndex();
layers[pos].reset();
add.setToolTipText("");
if(te[pos] != null) {
te[pos].getFrame().dispose();
te[pos] = null;
}
}
});
select.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pos = layerBox.getSelectedIndex();
if(layers[pos].getImage() != null) {
for(int i = 0; i < layers.length; i++) {
layers[i].setSelected(false);
}
layers[pos].setSelected(true);
localWidth.setValue(layers[pos].getWidth());
localHeight.setValue(layers[pos].getHeight());
posx.setValue(layers[pos].getX());
posy.setValue(layers[pos].getY());
}
}
});
edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pos = layerBox.getSelectedIndex();
if(layers[pos].getImage() != null) {
if(te[pos] != null) {
te[pos].getFrame().setVisible(true);
}
}
}
});
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
pos = layerBox.getSelectedIndex();
jfc = new JFileChooser();
jfc.setCurrentDirectory(new java.io.File("user.home"));
jfc.setDialogTitle("Select Layer 0 Image File");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (jfc.showOpenDialog(add) == JFileChooser.APPROVE_OPTION) {
try {
layers[pos].setFile(jfc.getSelectedFile());
if(!layers[pos].isReversed())
if(layers[pos].getFile().getName().contains(".txt")) {
if(te[pos] == null)
te[pos] = new TextEditor(pos);
else
te[pos].getFrame().setVisible(true);
// load defaults, this will be overriden when saved
layers[pos].setFont(te[pos].getFont());
layers[pos].setAlignment(te[pos].getAlignment());
layers[pos].setSize(te[pos].getSize());
layers[pos].setColor(te[pos].getColor()[0], te[pos].getColor()[1], te[pos].getColor()[2]);
layers[pos].setBold(te[pos].isBold());
layers[pos].setItalic(te[pos].isItalic());
layers[pos].setAdjusted(te[pos].isAdjusted());
layers[pos].setWidth(te[pos].getWidth());
layers[pos].setHeight(te[pos].getHeight());
layers[pos].setImage(convertTextToImage(layers[pos].getFile(), pos));
} else
if(layers[pos].getFile().getName().contains("png") ||
layers[pos].getFile().getName().contains("jpg") ||
layers[pos].getFile().getName().contains("jpeg") ||
layers[pos].getFile().getName().contains("bmp")){
layers[pos].setImage(ImageIO.read(layers[pos].getFile()));
layers[pos].setX(0);
layers[pos].setY(0);
layers[pos].setWidth((int) (layers[pos].getImage().getWidth()));
layers[pos].setHeight((int) (layers[pos].getImage().getHeight()));
} else {
JOptionPane.showMessageDialog(null, "txt, png, jpg, and bmp files only!");
layers[pos].reset();
add.setToolTipText("");
if(te[pos] != null) {
te[pos].getFrame().dispose();
te[pos] = null;
}
return;
}
else {
reverseImage(pos);
}
edit.setEnabled(true);
add.setToolTipText(jfc.getSelectedFile().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
pause = false;
deselect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
for(int i = 0; i < numberOfLayers; i++) {
layers[i].setSelected(false);
}
}
});
generate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
generatePanel(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
posx.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
for(int i = 0; i < layers.length; i++) {
if(layers[i].isSelected()) {
layers[i].setX((int) posx.getValue());
}
}
}
});
posy.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
for(int i = 0; i < layers.length; i++) {
if(layers[i].isSelected()) {
layers[i].setY((int) posy.getValue());
}
}
}
});
localWidth.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
for(int i = 0; i < layers.length; i++) {
if(layers[i].isSelected())
layers[i].setWidth((int) localWidth.getValue());
}
}
});
localHeight.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(!ignore) {
for(int i = 0; i < layers.length; i++) {
if(layers[i].isSelected())
layers[i].setHeight((int) localHeight.getValue());
}
} else {
ignore = false;
}
}
});
}
public void addNotify() {
preview.addNotify();
preview.requestFocus();
preview.setFocusable(true);
preview.setVisible(true);
if(thread == null) {
thread = new Thread(this);
preview.addKeyListener(this);
preview.addMouseListener(this);
thread.start();
}
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_ARGB);
g = (Graphics2D) image.getGraphics();
running = true;
}
private long redraw() {
long t = System.currentTimeMillis();
if(onTab) {
if(!FileManager.isSleeping())
update();
if(!pause) {
draw();
//drawToScreen();
}
}
return System.currentTimeMillis() - t;
}
public void run() {
init();
while(running) {
long durationMs = redraw();
try {
Thread.sleep(Math.max(0, FPS - durationMs));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void update () {
reupdateImages();
}
private void draw() {
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
for(int i = 0; i < layers.length; i++) {
if(layers[i] != null) {
g.drawImage(
layers[i].getImage(),
layers[i].getX(),
layers[i].getY(),
layers[i].getWidth(),
layers[i].getHeight(),
null
);
g.setColor(Color.red);
for(int j = 0; j < layers.length; j++) {
if(layers[j].isSelected())
g.drawRect(
layers[j].getX(),
layers[j].getY(),
layers[j].getWidth(),
layers[j].getHeight()
);
}
}
}
}
private void drawToScreen() {
System.out.println(preview);
System.out.println(preview.getGraphics());
Graphics g2 = preview.getGraphics();
g2.drawImage(image, 0, 0,
WIDTH, HEIGHT,
null);
panelImage = image;
g2.dispose();
}
public BufferedImage generateThumbnail() {
// so draw doesnt interfere
if(!pause) pause = true;
for(int i = 0; i < numberOfLayers; i++) {
layers[i].setSelected(false);
}
draw();
drawToScreen();
int genwidth = Integer.parseInt(width.getText());
int genheight = Integer.parseInt(height.getText());
// manipulate the width and height to specs
BufferedImage resized = new BufferedImage(genwidth, genheight, BufferedImage.TYPE_INT_ARGB);
Graphics g = resized.createGraphics();
g.drawImage(panelImage, 0, 0, genwidth, genheight, null);
g.dispose();
pause = false;
return resized;
}
public void deselect() {
for(int i = 0; i < numberOfLayers; i++) {
layers[i].setSelected(false);
}
}
private void generatePanel(String n) throws IOException {
if(!pause) pause = true;
for(int i = 0; i < numberOfLayers; i++) {
layers[i].setSelected(false);
}
draw();
drawToScreen();
String user = System.getProperty("user.name");
String location = FileManager.getMediaDirectory().replaceAll("/", "\\\\") + "\\";
int genwidth = WIDTH;
int genheight = HEIGHT;
// manipulate the width and height to specs
BufferedImage resized = new BufferedImage(genwidth, genheight, BufferedImage.TYPE_INT_ARGB);
Graphics g = resized.createGraphics();
g.drawImage(panelImage, 0, 0, genwidth, genheight, null);
g.dispose();
//print
File outputfile = null;
if(n == null || n == "")
outputfile = new File(location + "\\test.png");
else
outputfile = new File(n);
pause = false;
ImageIO.write(resized, "png", outputfile);
}
public static void reupdateImagesOverride() {
/* basically, this will update any changes thus
* if a char change happened, it will change ONLY if you are
* using root as your media center
*/
for(int i = 0; i < layers.length; i++) {
if(layers[i] != null && layers[i].getFile() != null && layers[i].getImage() != null) {
try {
if(!layers[i].isReversed()) {
if(layers[i].getFile().getName().contains(".txt")) {
layers[i].setImage(convertTextToImage(layers[i].getFile(), i));
layers[i].setWidth(layers[i].getImage().getWidth());
layers[i].setHeight(layers[i].getImage().getHeight());
} else
layers[i].setImage(ImageIO.read(layers[i].getFile()));
} else {
reverseImage(i);
}
try{
layers[i].collectTimeStamp();
} catch (Exception e2) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void reupdateImages() {
/* basically, this will update any changes thus
* if a char change happened, it will change ONLY if you are
* using root as your media center
*/
for(int i = 0; i < layers.length; i++) {
if(layers[i] != null && layers[i].getFile() != null && layers[i].getImage() != null) {
if(layers[i].getFile().lastModified() != layers[i].getTimeStamp()) {
try {
if(!layers[i].isReversed()) {
if(layers[i].getFile().getName().contains(".txt")) {
layers[i].setImage(convertTextToImage(layers[i].getFile(), i));
layers[i].setWidth(layers[i].getImage().getWidth());
layers[i].setHeight(layers[i].getImage().getHeight());
} else
layers[i].setImage(ImageIO.read(layers[i].getFile()));
} else {
reverseImage(i);
}
try{
layers[i].collectTimeStamp();
} catch (Exception e2) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// really simple
public static BufferedImage convertTextToImage(File f, int i) {
try {
int tmpy = 0;
int type = Font.PLAIN;
if(layers[i].isBold()) type = Font.BOLD;
if(layers[i].isItalic()) type = type | Font.ITALIC;
layers[i].setFont(te[i].getFont());
layers[i].setAlignment(te[i].getAlignment());
layers[i].setSize(te[i].getSize());
layers[i].setColor(te[i].getColor()[0], te[i].getColor()[1], te[i].getColor()[2]);
layers[i].setBold(te[i].isBold());
layers[i].setItalic(te[i].isItalic());
layers[i].setAdjusted(te[i].isAdjusted());
layers[i].setWidth(te[i].getWidth());
layers[i].setHeight(te[i].getHeight());
// grab width of longest line, if it's multi-line
BufferedImage tmp = new BufferedImage(layers[i].getWidth(), layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage actual;
BufferedImage ghetto;
Graphics2D gx = tmp.createGraphics();
gx.setColor(new Color(layers[i].getColor()[0], layers[i].getColor()[1], layers[i].getColor()[2]));
gx.setFont(new Font(layers[i].getFont(), type, layers[i].getSize()));
String line = null;
BufferedReader reader = new BufferedReader(new FileReader(f));
int longest = 0;
while((line = reader.readLine()) != null) {
if(gx.getFontMetrics().stringWidth(line) > longest) {
longest = gx.getFontMetrics().stringWidth(line);
}
}
// check if the image/text is longer then designated width
if(longest > layers[i].getWidth()) {
if(layers[i].isAdjusted()) {
int tmpnum = longest - layers[i].getWidth();
int reduce = 0;
while(tmpnum > sensitivity) {
tmpnum %= sensitivity;
reduce++;
}
for(int z = 0; z < overrideSizes.length; z++) {
if(overrideSizes[z] > layers[i].getSize()) {
if(z - reduce >= 0) {
layers[i].setSize(overrideSizes[z - reduce]);
if(layers[i].getAlignment().equals("right") || layers[i].getAlignment().equals("center")) {
if(z - (reduce + 1) >= 0) layers[i].setSize(overrideSizes[z - (reduce + 1)]);
}
} else {
layers[i].setSize(overrideSizes[0]);
}
break;
}
}
}
// draw to image, ignore controllers wish of width, we will do that later
actual = new BufferedImage(longest, layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
gx.dispose();
tmp = null;
gx = actual.createGraphics();
gx.setColor(new Color(te[i].getColor()[0], te[i].getColor()[1], te[i].getColor()[2]));
gx.setFont(new Font(layers[i].getFont(), type, layers[i].getSize()));
reader.close();
reader = new BufferedReader(new FileReader(f));
while((line = reader.readLine()) != null) {
gx.drawString(line,0, (tmpy += gx.getFontMetrics().getHeight()));
}
reader.close();
// now lets resize this
ghetto = new BufferedImage(layers[i].getWidth(), layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2x = ghetto.createGraphics();
g2x.drawImage(actual, 0, 0, layers[i].getWidth(), layers[i].getHeight(), null);
g2x.dispose();
return ghetto;
} else {
actual = new BufferedImage(layers[i].getWidth(), layers[i].getHeight(), BufferedImage.TYPE_INT_ARGB);
gx.dispose();
tmp = null;
gx = actual.createGraphics();
gx.setColor(new Color(te[i].getColor()[0], te[i].getColor()[1], te[i].getColor()[2]));
gx.setFont(new Font(layers[i].getFont(), type, layers[i].getSize()));
reader.close();
reader = new BufferedReader(new FileReader(f));
while((line = reader.readLine()) != null) {
if(layers[i].getAlignment().equals("left"))
gx.drawString(line,0, (tmpy += gx.getFontMetrics().getHeight()));
else
if(layers[i].getAlignment().equals("right"))
gx.drawString(line, layers[i].getWidth() - gx.getFontMetrics().stringWidth(line), (tmpy += gx.getFontMetrics().getHeight()));
else
if(layers[i].getAlignment().equals("center"))
gx.drawString(line, (layers[i].getWidth() / 2) - (gx.getFontMetrics().stringWidth(line) / 2), (tmpy += gx.getFontMetrics().getHeight()));
}
reader.close();
return actual;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static void reverseImage(int i) {
BufferedImage tmp = new BufferedImage(
layers[i].getImage().getWidth(),
layers[i].getImage().getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics gx = tmp.createGraphics();
try {
if(layers[i].getFile().getName().contains(".txt")) {
layers[i].setImage(convertTextToImage(layers[i].getFile(), i));
} else
layers[i].setImage(ImageIO.read(layers[i].getFile()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gx.drawImage(
layers[i].getImage(),
layers[i].getImage().getWidth(),
0,
-layers[i].getImage().getWidth(),
layers[i].getImage().getHeight(),
null);
gx.dispose();
layers[i].setImage(tmp);
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
// button is being held, scanning layers that match
if(arg0.getButton() == MouseEvent.BUTTON3) {
for(int i = 0; i < layers.length; i++) {
if(layers[i].getFile() != null && layers[i].getImage() != null && layers[i].isSelected()) {
if(layers[i].isReversed()) {
layers[i].setReversed(false);
reupdateImagesOverride();
return;
} else {
layers[i].setReversed(true);
reupdateImagesOverride();
return;
}
}
}
}
if(arg0.getButton() == MouseEvent.BUTTON1) {
for(int i = 0; i < layers.length; i++) {
if(layers[i].isSelected()) {
layers[i].setX(arg0.getX());
layers[i].setY(arg0.getY());
posx.setValue(arg0.getX());
posy.setValue(arg0.getY());
}
}
}
}
public void mouseReleased(MouseEvent arg0) {
}
public void keyPressed(KeyEvent arg0) {
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public void setOnTab(boolean b) {
onTab = b;
}
}
最佳答案
不要覆盖 addNotify!
JPanel
仅具有 Graphics
实例,并且仅当将其添加到父容器时。
Graphics
实例是在 Component.addNotify()
期间构建的,但您覆盖了它,从而阻止了它的创建。
也许尝试调用 super.addNotify() 作为快速解决方法。
关于java - getGraphics() 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41794092/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!