我不斷收到以下錯誤消息java.lang.IllegalStateException:緩衝區尚未創建。 這是我第一次遇到這個,不知道如何安全地擺脫它。 我研究了這個錯誤消息,我唯一的解決方案是使用以下內容:我不斷收到以下錯誤消息java.lang.IllegalStateException:緩衝區尚未創建
createBufferStrategy(2);
從我的理解來看,默認情況下Java是雙緩衝,我不需要上面的行。 (順便說一下,我在錯誤發生之前並沒有在所有的 處弄到緩衝區)。有人說,如果你在美國東部時間之外渲染某些東西,可能會顯示此消息。
以下是我寫的示例代碼,用於演示我的應用程序的功能。首先,該應用程序寫在NetBeans平臺 (RCP)上。 BarcodeInput類接收通過連接到串行端口的條形碼掃描器進入的數據,而不是工作人員BarcodeProcessWorker進一步處理該數據。下面顯示的代碼的工作方式很好,不會給我錯誤消息,但如果我使用SwingUtilities.invokeLater,則會收到錯誤消息。 這對我來說沒有任何意義,因爲我認爲我應該使用後臺線程中的SwingUtilities.invokeLater來更新GUI,因爲所有更新都應該在EDT上執行。
public final class MainTopComponent extends TopComponent {//NetBeans TopComponent
public MainTopComponent() {
initComponents();//The MainJPanel is created here
...
}
}
public class MainJPanel extends javax.swing.JPanel{
private SerialPortConnection spc;
public MainJPanel() {
initComponents();//Paints GUI components
initSerialPortConnection();//Initialize the Serial Port (SerialPortConnection spc) for the Barcode Scanner
initBarcode();
}
private void initBarcode(){
BarcodeInput bsi = new BarcodeInput(bspc);
}
}
public class BarcodeInput implements BarcodeSerialPortListener{
private BarcodeSerialPortConnection bspc;
private BarcodeJDialog barcodeJDialog;
public BarcodeInput(BarcodeSerialPortConnection bspc) {
this.bspc = bspc;
barcodeJDialog = new barcodeJDialog();//This is just a regular JDialog with some labels displayed and among them is a label called labelBarcode
}
public void addToBarcodeSerialPortListenerList() {
bspc.addToBarcodeSerialPortListenerList(this);
}
public boolean isBarcodeJDialogVisible(){
return barcodeJDialog.isVisible();
}
public void showBarcodeJDialog(){
barcodeJDialog.setVisible(true);
}
public void hideBarcodeJDialog(){
barcodeJDialog.setVisible(false);
}
public void setTextOfLabelBarcode(String s){
barcodeJDialog.setTextOfLabelBarcode(s);
}
@Override
public void stringReveivedFromSerialPort(String serialPortString) {//Overrides the method in the interface BarcodeSerialPortListener
BarcodeProcessWorker bpWorker = new BarcodeProcessWorker(this, serialPortString);
bpWorker.execute();//This swing worker class processes the received serial port string
}
}
public class BarcodeProcessWorker extends SwingWorker<Void,Void>{
private BarcodeInput bi;
private String serialPortString;
public BarcodeProcessWorker(BarcodeInput bi, String serialPortString){
this.bi = bi;
this.serialPortString = serialPortString;
}
@Override
protected Void doInBackground() throws Exception{
if("SHOW".contains(serialPortString)){
//First I remove the SHOW from the serialPortString and whatever is left is the data:
//For example if the barcode had SHOW12345, I end up with 12345 as data. SHOW is just a command
//that tells my application to display the BarcodeJDialog, where HIDE tells it to hide the BarcodeJDialog.
ShowBarcode sb = new ShowBarcode(bi, barcodeData);
sb.show();
}else if("HIDE".equals(serialPortString){
bi.hideBarcodeJDialog();//WORKS LIKE THIS, DOES NOT WORK AS SHOWN BELOW
//If I use the following here, I get that error message
// SwingUtilities.invokeLater(new Runnable(){
// @Override
// public void run(){
// bi.hideBarcodeJDialog();
// }
// });
}
return null;
}
}
public class ShowBarcode {
private BarcodeInput bi;
private String barcodeData;
public ShowBarcode(BarcodeInput bi, String barcodeData){
this.bi = bi;
this.barcodeData = barcodeData;
}
public void show(){
bi.setTextOfLabelBarcode(barcodeData);//Set text as 12345
bi.showBarcodeJDialog();//WORKS LIKE THIS, DOES NOT WORK AS SHOWN BELOW
//If I use the following here, I get that error message
// SwingUtilities.invokeLater(new Runnable(){
// @Override
// public void run(){
// bi.setTextOfLabelBarcode(barcodeData);//Set text as 12345
// bi.showBarcodeJDialog();
// }
// });
}
}
考慮修剪您的代碼以生成[SSCCE](http://sscce.org)。 –